Determining whether a class implements a generic list in a T4 template

Posted by James Hollingworth on Stack Overflow See other posts from Stack Overflow or by James Hollingworth
Published on 2010-06-09T11:46:01Z Indexed on 2010/06/09 12:52 UTC
Read the original article Hit count: 152

Filed under:
|
|
|

I'm writing a T4 template which loads some classes from an assembly, does some analysis of the classes and then generates some code. One particular bit of analysis I need to do is to determine whether the class implements a generic list. I can do this pretty simply in C#, e.g.

public class Foo : List<string> { }

var t = typeof(Foo);

if (t.BaseType != null && t.BaseType.IsGenericType && t.BaseType.GetGenericTypeDefinition() == typeof(List<>)))
    Console.WriteLine("Win");

However T4 templates use the FXCop introspection engine and so you do not have access to the .net reflection API. I've spent the past couple of hours in Reflector but still can't figure it out. Does anyone have any clues about how to do this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about templates