[Mono-list] Obtaining a generic MonoMethod

Andreas Färber andreas.faerber at web.de
Tue Dec 5 05:50:10 EST 2006


Hi,

Am 04.12.2006 um 10:08 schrieb Robert Jordan:

>>>> I would like to invoke the generic method IList<T>.Add(T) on an
>>>> object - how can I do this?
>>
>> obtaining a non-zero ICollection`1 MonoClass*, I get a non-
>> zero mono_class_get_method_from_name(cls, "Add", 1) MonoMethod*
>> ("System.Collections.Generic.ICollection`1:Add (T)") - but then if I
>> do mono_object_get_virtual_method, mono_method_full_name on the
>> returned MonoMethod* says it's System.Object:Finalize()!

>> So please consider it a general question: How do I correctly invoke a
>> generic method? ;-)
>
> You call methods of generic classes like every other method.
> The "trick" is to get the method from the "inflated" class,
> which you can obtain from mono_object_get_class ().
>
> MonoClass *clazz = mono_object_get_class (obj);

I'm not so happy with this solution. I thought of this but it's not  
as simple as that! ;-)

I figured out I always have to use the exact MonoClass the method is  
declared on. So the above solution will only work if the MonoClass  
returned by mono_object_get_class was in fact the one implementing  
the correct method (as opposed to e.g.: ICollection<T> ->  
IList<T>:ICollection<T> -> OneClass:IList<MyType> ->  
AnotherClass:OneClass), also this messes with overridden/new methods  
as I wouldn't be using mono_object_get_virtual_method that way.


The documentation has some names of functions surrounding generics  
but they're not really documented:

MonoType*
mono_class_inflate_generic_type (MonoType *type, MonoGenericContext  
*context);

MonoMethod*
mono_class_inflate_generic_method (MonoMethod *method,  
MonoGenericContext *context);

MonoMethod *
mono_get_inflated_method (MonoMethod *method);

With the parameter being on the interface, do I need to inflate the  
class or the method or both? Calling mono_get_inflated_method for my  
ICollection`1:Add MonoMethod simply returns my argument! Virtually  
all other functions have MonoGenericContext arguments - how do I  
obtain such a context? MonoGenericContext is typedef'ed as struct  
_MonoGenericContext and I don't see any helper functions to create it  
or work with its non-declared members.

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20061205/92ad2582/attachment-0001.html 


More information about the Mono-list mailing list