[Mono-dev] Detect List<> from C code
Robert Jordan
robertj at gmx.net
Sat Jun 20 19:24:29 EDT 2009
Joachim Ante wrote:
> 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.
mono_class_get_name () of *any* List<T>, including List<>
is indeed "System.Collections.Generic.List`1", but I won't
rely on this string because it may change in the future.
For example, a better name for List<int> would be
"System.Collections.Generic.List`1[System.Int32]"
while "System.Collections.Generic.List`1" should be
reserved for the generic type definition (List<T>).
I don't know if the current behavior is bug, oversight
or a feature.
> 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.
Robert
> 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