This project is read-only.

ITypeReference from System.Type

Topics: Metadata Model
May 6, 2011 at 6:07 AM


Is there a way to create ITypeReference to type that I have instance of System.Type without handling all cases for PlatformType.CreateReference? I have managed to make it work for not generic namespace or nested types (with custom handling by NestedTypeReference) Now I need to create type reference to generic type with concrete generic arguments and I cannot get it to work - emit executable code.



May 6, 2011 at 6:59 AM

This is not currently a supported scenario. Can you say more about the problem you are trying to solve?

May 6, 2011 at 5:55 PM


I had a similar situation, but I ended up creating a method to determine the ITypeDefinition instead, which works for me and would work in your case but ultimately is likely less efficient than creating a reference directly.  I would also be interesting in a clean and complete way of obtaining an ITypeReference for a given System.Type as well.  Here is a link to my implementation:

In terms of Herman's question regarding the problem scenario, I think there would be great benefits to having a corollary to Microsoft.Cci.ReflectionEmitter.ReflectionMapper class, which instead maps .NET concepts to CCI concepts.  This is one of the core problems/design challenges for the Afterthought project I am working on, as I am trying to allow users to leverage CCI while only interacting with strongly-typed code and a bit of System.Reflection which they are likely more accustomed to.  For this reason, I have a number of Resolve methods that convert System.Reflection types into Microsoft.Cci types.  However, I am extremely confident that my implementations are not comprehensive because I lack the deep breath of knowledge of difference scenarios that people like Herman have.

May 9, 2011 at 8:53 AM
Edited May 9, 2011 at 8:57 AM


For now I managed to create reference to generic type and a method in it but I have problem emitting method call to it:


interface NonGeneric{
    void DoSth();

interface Generic<T>{
    void DoSth(T arg); //to my knowledge this is not generic method 
so I use the same mechanism for retrieving its reference as
any other in my project (other calls work)
} class Implementation : Generic<T>,NonGeneric{ //... }

I am able to emit working call to Implementation.DoSth() by method reference obtained form interface but my attempts to do this analogically with the generic interface failed. So I changed approach and seek method on Implementation and that call works. (I fear to implement interface explicitly and see what happens :) )


My implementation of class that resolves reference from Type, MethodBase and some expression helpers: of course this is not complete it is what I currently need.

May 9, 2011 at 3:22 PM

To call methods obtained from generic types, you need to have ISpecializedMethodReference objects.