Methods are represented by the MonoMethod*
instances. Various APIs surface these instances, but usually
you will use the method description
API to get a handle to a Mono Method. You can invoke those methods from C,
or you can probe probe various properties of a method, and in particular
its method signature or get
some low-level information about them.
The following code snippet from the Mono runtime shows you
how to create a managed System.Version
instance
with four integers by looking up the constructor in the
managed implementation of System.Version, creating an instance
of the object, and then invoking the constructor on it.
Methods are represented by the MonoMethod*
instances. To simplify the process of getting
a MonoMethod*
, you use Method Descriptors, which
are C-strings that describe the method that you are looking
for, and then you perform a search in either
a type, or a
loaded image.
To describe a method, you use the Method Description API. Method descriptions are used to locate a method in the executing program. They can either be fully specified, that is, they would include the namespace, class, method name and all of its arguments or they omit the namespace and arguments and even use wildcard matches for the class name and method names.
You use the fully specified version to identify a particular method, or you can use the partial version to find a method or a family of methods in the code.
Method descriptions are typically created from a C string representation, and take the following form:
[namespace.]classname:methodname[(args...)]
Both classname and methodname can contain the '*' character which can be used to match anything. Arguments are separated by commas.
You can use the type shortcuts to match the fully qualified
parameter types. The supported type shortcuts are:
char
,
bool
,
byte
,
sbyte
,
uint16
,
int16
,
uint
,
int
,
ulong
,
long
,
uintptr
,
intptr
,
single
,
double
,
string
and
object
.
The type parameters can use the "&" and "*" type modifiers.
Examples of method descriptions:
You can then search for methods in MonoImages or search for methods in classes.
mono_method_desc_new
name | the method name. |
include_namespace | whether the name includes a namespace or not. |
Creates a method description for name, which conforms to the following specification:
[namespace.]classname:methodname[(args...)]
in all the loaded assemblies.
Both classname and methodname can contain '*' which matches anything.
mono_method_desc_free
desc | method description to be released |
Releases the MonoMethodDesc object desc.
mono_method_desc_from_method
mono_method_desc_full_match
mono_method_desc_match
mono_method_desc_search_in_class
mono_method_full_name
mono_method_get_class
mono_method_get_flags
mono_method_get_last_managed
mono_method_get_marshal_info
mono_method_get_name
mono_method_get_object
mono_method_get_param_names
mono_method_get_param_token
mono_method_get_signature
mono_method_get_index
mono_method_get_signature_full
mono_method_get_token
mono_method_get_unmanaged_thunk
method | method to generate a thunk for. |
Returns an unmanaged->managed thunk that can be used to call a managed method directly from C.
The thunk's C signature closely matches the managed signature:
C#: public bool Equals (object obj); C: typedef MonoBoolean (*Equals)(MonoObject*, MonoObject*, MonoException**);
The 1st ("this") parameter must not be used with static methods:
C#: public static bool ReferenceEquals (object a, object b); C: typedef MonoBoolean (*ReferenceEquals)(MonoObject*, MonoObject*, MonoException**);
The last argument must be a non-null pointer of a MonoException* pointer.
It has "out" semantics. After invoking the thunk, *ex will be NULL
if no
exception has been thrown in managed code. Otherwise it will point
to the MonoException* caught by the thunk. In this case, the result of
the thunk is undefined:
MonoMethod *method = ... // MonoMethod* of System.Object.Equals
MonoException *ex = NULL
;
Equals func = mono_method_get_unmanaged_thunk (method);
MonoBoolean res = func (thisObj, objToCompare, &ex);
if (ex) {
// handle exception
}
The calling convention of the thunk matches the platform's default convention. This means that under Windows, C declarations must contain the __stdcall attribute:
C: typedef MonoBoolean (__stdcall *Equals)(MonoObject*, MonoObject*, MonoException**);
LIMITATIONS
Value type arguments and return values are treated as they were objects:
C#: public static Rectangle Intersect (Rectangle a, Rectangle b); C: typedef MonoObject* (*Intersect)(MonoObject*, MonoObject*, MonoException**);
Arguments must be properly boxed upon trunk's invocation, while return values must be unboxed.
mono_method_has_marshal_info
mono_runtime_invoke
method | method to invoke |
obJ | object instance |
params | arguments to the method |
exc | exception information. |
Invokes the method represented by method on the object obj.
obj is the 'this' pointer, it should be NULL
for static
methods, a MonoObject* for object instances and a pointer to
the value type for value types.
The params array contains the arguments to the method with the same convention: MonoObject* pointers for object instances and pointers to the value type otherwise.
From unmanaged code you'll usually use the mono_runtime_invoke() variant.
Note that this function doesn't handle virtual methods for you, it will exec the exact method you pass: we still need to expose a function to lookup the derived class implementation of a virtual method (there are examples of this in the code, though).
You can pass NULL
as the exc argument if you don't want to
catch exceptions, otherwise, *exc will be set to the exception
thrown, if any. if an exception is thrown, you can't use the
MonoObject* result from the function.
If the method returns a value type, it is boxed in an object reference.
mono_runtime_invoke_array
method | method to invoke |
obJ | object instance |
params | arguments to the method |
exc | exception information. |
Invokes the method represented by method on the object obj.
obj is the 'this' pointer, it should be NULL
for static
methods, a MonoObject* for object instances and a pointer to
the value type for value types.
The params array contains the arguments to the method with the same convention: MonoObject* pointers for object instances and pointers to the value type otherwise. The _invoke_array variant takes a C# object[] as the params argument (MonoArray *params): in this case the value types are boxed inside the respective reference representation.
From unmanaged code you'll usually use the mono_runtime_invoke_checked() variant.
Note that this function doesn't handle virtual methods for you, it will exec the exact method you pass: we still need to expose a function to lookup the derived class implementation of a virtual method (there are examples of this in the code, though).
You can pass NULL
as the exc argument if you don't want to
catch exceptions, otherwise, *exc will be set to the exception
thrown, if any. if an exception is thrown, you can't use the
MonoObject* result from the function.
If the method returns a value type, it is boxed in an object reference.
mono_runtime_delegate_invoke
delegate | pointer to a delegate object. |
params | parameters for the delegate. |
exc | Pointer to the exception result. |
Invokes the delegate method delegate with the parameters provided.
You can pass NULL
as the exc argument if you don't want to
catch exceptions, otherwise, *exc will be set to the exception
thrown, if any. if an exception is thrown, you can't use the
MonoObject* result from the function.
mono_method_signature
Return the signature of the method M. On failure, returns NULL
.
mono_signature_explicit_this
sig | the method signature inspected |
TRUE
if this the method signature sig has an explicit
instance argument. #FALSE
otherwise.
mono_signature_get_call_conv
sig | the method signature inspected |
mono_signature_get_desc
mono_signature_get_param_count
sig | the method signature inspected |
mono_signature_get_params
sig | the method signature inspected |
iter | pointer to an iterator |
NULL
when finished.
Iterates over the parameters for the method signature sig.
A void* pointer must be initualized to #NULL
to start the iteration
and it's address is passed to this function repeteadly until it returns
#NULL
.
mono_signature_get_return_type
sig | the method signature inspected |
mono_signature_hash
mono_signature_is_instance
sig | the method signature inspected |
TRUE
if this the method signature sig has an implicit
first instance argument. #FALSE
otherwise.
mono_signature_param_is_out
sig | the method signature inspected |
param_num | the 0-based index of the inspected parameter |
TRUE
if the parameter is an out parameter, #FALSE
otherwise.
mono_signature_vararg_start
sig | the method signature inspected |
mono_param_get_objects
mono_get_method_full
mono_method_get_header
mono_method_header_get_clauses
mono_method_header_get_code
mono_method_header_get_locals