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:

You can then search for methods in MonoImages or search for methods in classes.

mono_method_desc_new

mono_method_desc_free

mono_method_desc_from_method

mono_method_desc_full_match

mono_method_desc_match

mono_method_desc_search_in_class

mono_method_desc_search_in_image

Working with Methods

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

mono_method_has_marshal_info

mono_method_verify

Invoking Methods

mono_runtime_invoke

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

mono_runtime_delegate_invoke

mono_method_body_get_object

Method Signatures

mono_method_signature

mono_signature_explicit_this

mono_signature_get_call_conv

mono_signature_get_desc

mono_signature_get_param_count

mono_signature_get_params

mono_signature_get_return_type

mono_signature_hash

mono_signature_is_instance

mono_signature_param_is_out

mono_signature_vararg_start

mono_param_get_objects

mono_get_method_full

mono_get_method

Methods Header Operations

mono_method_get_header

mono_method_header_get_clauses

mono_method_header_get_code

mono_method_header_get_locals

mono_method_header_get_num_clauses