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

Joachim Ante joe at unity3d.com
Sat Jun 20 18:42:55 EDT 2009


Hi,

Olexandr pointed out that the class name is always "List`1". So it  
seems that comparing the class name also checking against the mono  
image of the class against mscorlib is a safe way of checking if it  
is System.Generic.Container.List.

Now i have the generic type from the field in a MonoClass* pointer,  
how do I correctly create an instance from the class?

mono_object_new and mono_runtime_object_init doesn't seem to work.  
The values don't seem to be correctly initialized when i use those  
two functions on the MonoClass extracted from the MonoClassField.

Joachim Ante


> Joachim Ante wrote:
>> Hi,
>>
>> Whats the best way of checking if a MonoField* refers to a
>> System.Generic.Container.List<>? The check needs to be high  
>> performance.
>
> The public API does not expose the necessary functions to
> achieve this, but you could use a managed helper:
>
> public static bool IsGenericTypeOf (Type genericInstance, Type
> genericDefinition)
> {
> 	if (!genericInstance.IsGenericType)
> 		return false;
>
> 	if (!genericDefinition.IsGenericTypeDefinition)
> 		return false;
> 		
> 	return genericInstance.GetGenericTypeDefinition() ==
> 		genericDefinition;
> }
>
>
> When you invoke this method, the type arguments are actually
> MonoReflectionType*s which you can get from a class using
> this code:
>
> mono_type_get_object(mono_class_get_type (class))
>
> Robert
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list



More information about the Mono-devel-list mailing list