[Mono-dev] Detect List<> from C code

Joachim Ante joe at unity3d.com
Sun Jun 21 09:59:58 EDT 2009


Hi,

>>> > Now i have the generic type from the field in a MonoClass*  
>>> pointer,
>>> > how do I correctly create an instance from the class?
>>>
>>> If the only thing you have is a List<T>, you cannot create
>>> a class from it because you need to know the T.
>>>
>>> But if your field's class is a *closed* generic type like
>>> List<int>, then mono_object_new () will work just fine.
>>>
>>> Yes, generics is a missing part of the embedding API and this is
>>> something to be addressed. One should be able to do things like
>>> get the instantiation vector or create a new instantation.
>> Hmm, so I have:
>> class MyClass
>> {
>>     List<int> a;
>> }
>> I iterate through it from c code and use mono_class_get_fields,  
>> mono_class_from_mono_type(mono_field_get_type(field))
>> Then i do mono_object_new and call mono_runtime_object_init on it.
>> Are you sure this should work? It generates an invalid object for  
>> me. Is it possible that using System.Activator.CreateInstance   
>> will  work better or does this internally do the exact same thing?
>
>
> It works. See the attached samples.
Oops. Yeah, i got one of the mono_object_set_value calls wrong. It's  
working beautifully now.
Thanks for the help.

Joachim Ante



More information about the Mono-devel-list mailing list