[Mono-dev] Mono.Cecil: Full names of generic types

Jb Evain mono at evain.net
Mon Jul 24 05:20:46 EDT 2006


Hi Matej,

Matej Urbas wrote:
> I'm working on code completion (generics support) in MonoDevelop and I'm 
> making extensive use of Mono.Cecil.

Great news!

> 2. Question about methods that have generic types as parameters: Here is 
> an example of such a method:
> 
> static T System.Array.FindLast <T> (T[] array, System.Predicate<T> match)
> 
> Now, the problem: Parameters are specified in MethodDefinition with a 
> collection of ParameterDefinition classes which have TypeReference as 
> their type. If we look at the 'match' parameter in the upper method, 
> Cecil will store this info in its m_paramType:
> 
> Full name of type: System.Predicate`1<T>
> GenericParameters: empty!

Here, the second parameter of the FindLast method is a predicate, which 
is a constructed generic type, which has as its first argument the first 
generic parameter of the method.

You can find constructed generic types (GenericInstanceType) and 
constructed generic methods (GenericInstanceMethod).

They both implement IGenericInstance wich provides a collection of 
argument. An argument is a TypeReference, which is the base class for 
any kinf of type (hence, it could be a TypeDefinition as well as another 
GenericInstanceType).

> You see, the GenericParameters collection of the 'match' parameter is 
> empty and its full name has the generic parameters already appended 
> (i.e.: <T>). Now, in monodevelop I have to extract generic parameters of 
> such parameters but it seems like there is no other way but to parse 
> them from the 'full name' string... In fact, one can tell that a method 
> parameter is generic only if one searches for a &gt; or &lt; character 
> in its name... I would really like to see those parameters specified in 
> a collection rather than appended to the string. What can I do about it?

You can check:

MethodReference ref = ...;
GenericInstanceMethod gim = ref as GenericInstanceMethod;
if (gim != null) {
	foreach (TypeReference argument in gim.Arguments) {
	}
}

> 3. Oh, and to what extent is Mono.Cecil compatible with 
> System.Reflection? E.g.: are the Mono.Cecil.GenericParamAttributes and 
> System.Reflection.GenericParameterAttributes cast safe? - I mean, can 
> they be cast from one-another and still preserve the expected information?

Check that the values are the same before doing so. In theory, it "may" 
work.

> Hope I'm not annoying.

Feel free to ask if you have other questions about Cecil!

Jb

> Thanks in ahead
> ---
> Matej
> _______________________________________________
> 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