[Mono-dev] Invoking method from base class in embedded environment

Robert Jordan robertj at gmx.net
Mon Aug 28 11:06:55 EDT 2006


Janne Rantala wrote:
> Hi,
> 
> I have two classes, simply Class1 and Class2. Class1 has only one
> method, public string get(). Class2 extends Class1.
> 
> Now when I create an instance of Class1 in embedded environment, and
> wish to invoke that get() -method, either function
> mono_class_get_method_from_name cannot find it and function
> mono_class_get_methods iterates only methods available in Class2
> (which in this case is .ctor).

I'm using these lookup methods:

MonoMethod *
mono_class_get_method_from_name_recursive (
	MonoClass *clazz, char *name, int param_count)
{
	MonoMethod *method = NULL;
	while (clazz != NULL && method == NULL) {
		method = mono_class_get_method_from_name (clazz, name, param_count);
		if (method == NULL)
			clazz = mono_class_get_parent (clazz);
	}
	return method;
}


Another one using MonoMethodDesc.

The signature format is

":methodname(argtype1,argtype2,...)"

see debug-helpers.[ch]

MonoMethod *
mono_class_get_method_from_desc_recursive (
	MonoClass *clazz, char *signature)
{
	MonoMethodDesc *desc;
	MonoMethod *method = NULL;

	desc = mono_method_desc_new (signature, TRUE);

	while (clazz != NULL && method == NULL) {
		method = mono_method_desc_search_in_class (desc, clazz);
		if (method == NULL)
			clazz = mono_class_get_parent (clazz);
	}
	mono_method_desc_free(desc);

	return method;
}


Before you invoke the method you should lookup
the possibly overridden method:

if (obj)
	method = mono_object_get_virtual_method (obj, method);

mono_runtime_invoke (method, obj, args, &exception);


Robert




More information about the Mono-devel-list mailing list