## Methods

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.
MonoObject* create_version (MonoDomain *domain, guint32 major, guint32 minor, guint32 build, guint32 revision) { MonoClass *System_Version; MonoMethod *create_version; MonoError error; MonoObject *result; gpointer args [4]; System_Version = mono_class_from_name (mono_defaults.corlib, "System", "Version"); // Create a method description that we use to search for the // constructor method MonoMethodDesc *desc = mono_method_desc_new (":.ctor(int,int,int,int)", FALSE); create_version = mono_method_desc_search_in_class (desc, System_Version); mono_method_desc_free (desc); // Setup the parameters to pass. args [0] = &major; args [1] = &minor; args [2] = &build; args [3] = &revision; // Create the object instance result = mono_object_new_checked (domain, System_Version, &error); // Raise an exception in case of an error mono_error_raise_exception (&error); // Otherwise, invoke the constructor mono_runtime_invoke (create_version, result, args, NULL); // Return ther esult return result; }

### Method Descriptions

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:

- "Monitor:Exit": matches classes and methods called "Monitor.Exit"
- "Monitor:enter_with_atomic_var(object,bool&)": matches a method in the class Monitor with two specific type parameters.
- ":.ctor(int,int,int,int)": matches constructors that take four integers.
- "System.Globalization.CultureInfo:CreateCulture(string,bool)": matches the CreateCultureMethod that takes a string and a boolean on the System.Globalization.CultureInfo class.

You can then search for methods in MonoImages or search for methods in classes.
`mono_method_desc_new`
Syntax
MonoMethodDesc* mono_method_desc_new (const char *name, gboolean include_namespace)

Parameters

name | the method name.
include_namespace | whether the name includes a namespace or not.

Return value
a parsed representation of the method description.
Description

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`
Syntax
void mono_method_desc_free (MonoMethodDesc *desc)

Parameters

desc | method description to be released

Description

Releases the MonoMethodDesc object desc.
`mono_method_desc_from_method`
Syntax
mono_method_desc_from_method

`mono_method_desc_full_match`
Syntax
mono_method_desc_full_match

`mono_method_desc_match`
Syntax
mono_method_desc_match

`mono_method_desc_search_in_class`
Syntax
mono_method_desc_search_in_class

`mono_method_desc_search_in_image`
Syntax
mono_method_desc_search_in_image

### Working with Methods

`mono_method_full_name`
Syntax
mono_method_full_name

`mono_method_get_class`
Syntax
mono_method_get_class

`mono_method_get_flags`
Syntax
mono_method_get_flags

`mono_method_get_last_managed`
Syntax
mono_method_get_last_managed

`mono_method_get_marshal_info`
Syntax
mono_method_get_marshal_info

`mono_method_get_name`
Syntax
mono_method_get_name

`mono_method_get_object`
Syntax
mono_method_get_object

`mono_method_get_param_names`
Syntax
mono_method_get_param_names

`mono_method_get_param_token`
Syntax
mono_method_get_param_token

`mono_method_get_signature`
Syntax
mono_method_get_signature

`mono_method_get_index`
Syntax
mono_method_get_index

`mono_method_get_signature_full`
Syntax
mono_method_get_signature_full

`mono_method_get_token`
Syntax
mono_method_get_token

`mono_method_get_unmanaged_thunk`
Syntax
gpointer mono_method_get_unmanaged_thunk (MonoMethod *method)

Parameters

method | method to generate a thunk for.

Description

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`
Syntax
mono_method_has_marshal_info

`mono_method_verify`
Syntax
mono_method_verify

### Invoking Methods

`mono_runtime_invoke`
Syntax
MonoObject* mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)

Parameters

method | method to invoke
obJ | object instance
params | arguments to the method
exc | exception information.

Description

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.
If you want to invoke generic methods, you must call the method on the "inflated" class, which you can obtain from the `mono_object_get_class()`
MonoClass *clazz; MonoMethod *method; clazz = mono_object_get_class (obj); /* * If there are more Add methods declared, you * may use mono_method_desc_search_in_class (clazz, ":Add(T)"), * you must substitute ":Add(T)" with the correct type, for example * for List<int>, you would use ":Add(int)". */ method = mono_class_get_method_from_name (clazz, "Add", 1); mono_runtime_invoke (method, obj, args, &exception);
`mono_runtime_invoke_array`
Syntax
MonoObject* mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params, MonoObject **exc)

Parameters

method | method to invoke
obJ | object instance
params | arguments to the method
exc | exception information.

Description

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`
Syntax
MonoObject* mono_runtime_delegate_invoke (MonoObject *delegate, void **params, MonoObject **exc)

Parameters

delegate | pointer to a delegate object.
params | parameters for the delegate.
exc | Pointer to the exception result.

Description

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`
Syntax
mono_method_body_get_object

### Method Signatures

`mono_method_signature`
Syntax
MonoMethodSignature* mono_method_signature (MonoMethod *m)

Description

Return the signature of the method M. On failure, returns `NULL`.
`mono_signature_explicit_this`
Syntax
gboolean mono_signature_explicit_this (MonoMethodSignature *sig)

Parameters

sig | the method signature inspected

Return value
#`TRUE` if this the method signature sig has an explicit instance argument. #`FALSE` otherwise.
Description

`mono_signature_get_call_conv`
Syntax
guint32 mono_signature_get_call_conv (MonoMethodSignature *sig)

Parameters

sig | the method signature inspected

Return value
the call convention of the method signature sig.
Description

`mono_signature_get_desc`
Syntax
mono_signature_get_desc

`mono_signature_get_param_count`
Syntax
guint32 mono_signature_get_param_count (MonoMethodSignature *sig)

Parameters

sig | the method signature inspected

Return value
the number of parameters in the method signature sig.
Description

`mono_signature_get_params`
Syntax
MonoType* mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)

Parameters

sig | the method signature inspected
iter | pointer to an iterator

Return value
the next parameter type of the method signature sig, #`NULL` when finished.
Description

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`
Syntax
MonoType* mono_signature_get_return_type (MonoMethodSignature *sig)

Parameters

sig | the method signature inspected

Return value
the return type of the method signature sig
Description

`mono_signature_hash`
Syntax
mono_signature_hash

`mono_signature_is_instance`
Syntax
gboolean mono_signature_is_instance (MonoMethodSignature *sig)

Parameters

sig | the method signature inspected

Return value
#`TRUE` if this the method signature sig has an implicit first instance argument. #`FALSE` otherwise.
Description

`mono_signature_param_is_out`
Syntax
mono_bool mono_signature_param_is_out (MonoMethodSignature *sig, int param_num)

Parameters

sig | the method signature inspected
param_num | the 0-based index of the inspected parameter

Return value
#`TRUE` if the parameter is an out parameter, #`FALSE` otherwise.
Description

`mono_signature_vararg_start`
Syntax
int mono_signature_vararg_start (MonoMethodSignature *sig)

Parameters

sig | the method signature inspected

Return value
the number of the first vararg parameter in the method signature sig. -1 if this is not a vararg signature.
Description

`mono_param_get_objects`
Syntax
mono_param_get_objects

`mono_get_method_full`
Syntax
mono_get_method_full

`mono_get_method`
Syntax
mono_get_method

### Methods Header Operations

`mono_method_get_header`
Syntax
mono_method_get_header

`mono_method_header_get_clauses`
Syntax
mono_method_header_get_clauses

`mono_method_header_get_code`
Syntax
mono_method_header_get_code

`mono_method_header_get_locals`
Syntax
mono_method_header_get_locals