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

Robert Jordan robertj at gmx.net
Sun Jun 21 09:36:32 EDT 2009


Joachim Ante wrote:
>> > 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.

Robert

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.cs
Url: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20090621/a68cf544/attachment.pl 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: teste.c
Type: text/x-csrc
Size: 1739 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20090621/a68cf544/attachment.bin 


More information about the Mono-devel-list mailing list