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 |
MonoMethodDesc
object desc.mono_method_desc_from_method
mono_method_desc_full_match
desc | A method description that you created with mono_method_desc_new |
method | a MonoMethod instance that you want to match against |
TRUE
if the specified method matches the specified description, FALSE
otherwise.
This method is used to check whether the method matches the provided description, by making sure that the method matches both the class and the method parameters.
mono_method_desc_match
desc | MonoMethoDescription |
method | MonoMethod to test |
TRUE
if the method matches the description, FALSE
otherwise.
Determines whether the specified method matches the provided desc description.
namespace and class are supposed to match already if this function is used.
mono_method_desc_search_in_class
mono_method_desc_search_in_image
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
domain | an app domain |
method | a method |
refclass | the reflected type (can be NULL ) |
System.Reflection.MonoMethod
object representing the method method.
mono_method_get_param_names
mono_method_get_param_token
mono_method_get_signature
_checked
variant
Notes: runtime code MUST not use this functionmono_method_get_index
MethodDef
table.mono_method_get_signature_full
call
IL instruction.
\deprecated use the _checked
variant
Notes: runtime code MUST not use this functionmono_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 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.EqualsMonoException *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_method_verify
mono_runtime_invoke
method | method to invoke |
obj | object instance |
params | arguments to the method |
exc | exception information. |
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. |
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*)
:
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_body_get_object
domain | an app domain |
method | a method |
System.Reflection.MethodBody/RuntimeMethodBody
object representing the method method.
mono_method_signature
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.void*
pointer must be initialized to NULL
to start the iteration
and its 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 |
-1
if this is not a vararg signature.mono_param_get_objects
mono_get_method_full
mono_get_method
mono_method_get_header
mono_method_header_get_clauses
header | a MonoMethodHeader pointer |
method | MonoMethod the header belongs to |
iter | pointer to a iterator |
clause | pointer to a MonoExceptionClause structure which will be filled with the info |
TRUE
if clause was filled with info, FALSE
if there are no more exception
clauses.
Get the info about the exception clauses in the method. Set *iter
to NULL
to
initiate the iteration, then call the method repeatedly until it returns FALSE
.
At each iteration, the structure pointed to by clause if filled with the
exception clause information.
mono_method_header_get_code
header | a MonoMethodHeader pointer |
code_size | memory location for returning the code size |
max_stack | memory location for returning the max stack |
Method header accessor to retreive info about the IL code properties: a pointer to the IL code itself, the size of the code and the max number of stack slots used by the code.
mono_method_header_get_locals
header | a MonoMethodHeader pointer |
num_locals | memory location for returning the number of local variables |
init_locals | memory location for returning the init_locals flag |
Method header accessor to retreive info about the local variables: an array of local types, the number of locals and whether the locals are supposed to be initialized to 0 on method entry