Find all TypeSpecifications / GenericTypeInstances in an assembly

43 views
Skip to first unread message

Kevin MacAulay Vacheresse

unread,
Jan 14, 2019, 5:13:42 PM1/14/19
to mono-cecil
Hello!

I'm trying to determine all GenericTypeInstances in an assembly. 

For example,

namespace TestProgram
{
    public class TestProgram
    {
        public struct GenericType<T>
          {
              public T mGeneric;
            public int mInt;
            public T mOtherGeneric;
            }

        public static Foo()
        {
            var instance1 = new GenericType<int>();
            var instance2 = new GenericType<char>();
        }

        public static int Main()
        {
            Foo();
            return 0;
        }
    }
}

I'm looking for a mechanism to ask, 'for this assembly, give me all generic type instances' and I would expect:
  • valuetype TestProgram.TestProgram.GenericType`1<int32>
  • valuetype TestProgram.TestProgram.GenericType`1<char>

The only approach I can think of currently is to inspect the IL across all method bodies and then if they refer to a generic type definition, that to a GenericTypeInstance and inspect the GenericArguments. But if there is a faster solution I am very interested.

Thanks and all the best,
Kev
Reply all
Reply to author
Forward
0 new messages