[Mono-dev] Extract MonoField

Robert Jordan robertj at gmx.net
Wed Jun 10 20:02:55 EDT 2009


Joachim Ante wrote:
> Hi,
> 
> I have this class on the C# side.
> class Test
> {
>     List<int> myValue;
> }
> 
>  From the embedding API I iterate through the list using  
> mono_class_get_fields.
> 
> When iterating through the Test class I need to call mono_object_new  
> or something similar, so that the it creates the equivalent of:
> new List<int> ();
> 
> 
> How do i do that? With non-generic classes I would just call
> klass = mono_type_get_class(mono_field_get_type(field));
> To extract the class and then pass it into mono_object_new. How do i  
> do that with generics. Doing the same as with non-generics seems to  
> just crash mono, so i presume there are some extra steps i have to do.

Get the reflection type (the System.Type equivalent) of the field:

MonoReflectionType *rtype =
   mono_type_get_object (mono_field_get_type (field));

and pass it to System.Activator.CreateInstance(Type)
using mono_runtime_invoke.

This is the only way to instantiate generic types using
the public embedding API. Same for generic methods.

Robert



More information about the Mono-devel-list mailing list