I want to be able to call methods on CLR classes from C++. In particular some classes may contain overloaded methods, so I need to search a class's methods based both on method name and parameter signature.
I have a function like this:
MonoMethod* find_method (
MonoDomain* domain,
MonoClass* type,
const char* name,
int nargs,
MonoClass** types)
I iterate over the members of the class, finding a matching member by name. I then need to check the parameters of the method and see whether they match the list required in the types parameter of this function.
mono_signature_get_params()
is then used to iterate over the parameters for each method with a matching name.
Questions: How can I do the following:
MonoClass*
constants for fundamental types int32_class, int64_class
, etc.
Here are 2 auxilliary functions that do not compile as when accessing fields in MonoType* or MonoClass*, the compiler complains indicating the two structures are incomplete:
//
// Detetermine whether classes A and B are equivalent
//
static bool IsEquivalent (MonoType* typeA, MonoType* typeB)
{
MonoTypeEnum Ta = typeA->type;
MonoTypeEnum Tb = typeB->type;
// if basic type not a match, can punt
if (Ta != Tb)
return false;
// if simple type, nothing further to check
if (Ta < MONO_TYPE_PTR)
return true;
// check class
if (Ta == MONO_TYPE_CLASS)
return typeA->data.klass = typeB->data.klass;
else
return typeA->data.klass = typeB->data.klass;
}
//
// Determine whether parameters in signature match incoming parameters
//
static bool types_match (
MonoMethodSignature* sig,
MonoClass** types,
int nargs)
{
void* iter = NULL;
MonoType* type = NULL;
int i = 0;
while (type = mono_signature_get_params (sig, &iter))
{
if (!IsEquivalent (type, types[i++]->this_arg))
return false;
}
return true;
}
You must not include the private mono headers: your app will break when we make changes to the internals.
To access the type enum value from a MonoType* you need to call the mono_type_get_type () function (from metadata/metadata.h). For MONO_TYPE_CLASS and MONO_TYPE_VALUETYPE you can access the MonoClass* value with mono_type_get_class ().
To compare two signatures for equality you can use mono_metadata_signature_equal(): note that using MonoClass* to represent an argument's type is fundamentally incorrect, since that can't represent, for example, an argument passed by reference.
If you need to match by assignability, you can use mono_class_is_assignable_from(), but note that you will need to deal with byref arguments, decide if you want to allow enum types to autoconvert to their underlying integer type etc.
The fundamental type classes can be trivially retrieved with functions like: mono_get_int32_class(), mono_get_boolean_class() etc.