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

Rodrigo Kumpera kumpera at gmail.com
Sat Jun 20 20:04:52 EDT 2009


On Sat, Jun 20, 2009 at 8:24 PM, Robert Jordan <robertj at gmx.net> wrote:

> 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.
>

You can rely on the name, as this is the canonical name of the type.
The "`1" part is to allow overloading on the number of generic arguments.
Such as .net 4.0, Tuple`1, Tuple`2 and so on.
This naming convention is part of the CIL spec and there is no reason t
change it.

But, how knows, someone could have the crazy idea of renaming a collection
in mscorlib and everyone be fine with it. ;)


>
> 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.


This is a feature, the type name is List`1 no matter which
instantiation is used.



>
> > 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.


Cheers,
Rodrigo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20090620/af6771e2/attachment.html 


More information about the Mono-devel-list mailing list