views:

53

answers:

2

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?

A: 

I downloaded Introspector from http://www.binarycoder.net/fxcop/ (as I mentioned in my comment) and it looks like you need to check BaseType or BaseClass Template.

Mark Hurd
Introspector unfortunately didn't give me any more information that Reflector.
James Hollingworth
A: 

Figured it out, it's not very pretty but all type's loaded using AssemblyNode.Load are of type TypeNode, to determine if the type implements List you must do this:

node.BaseType.IsGeneric && node.BaseType.Template == FrameworkAssemblies.Mscorlib.Types.SingleOrDefault(t => t.FullName == "System.Collections.Generic.List`1")

hope it helps someone!

James Hollingworth
Instead of matching by name, you could use FrameworkTypes.GenericList.
Nicole Calinoiu