I am trying to build a T4 template that will take the method definitions in an interface and reproduce the signature and call a base method with the passed parameters. The interface defines a multitude of methods so rewriting them every time the interface changes becomes very challenging. Another complication is the interface is a generic interface with possible generic methods and generic parameters. So far, the only way I can find to reproduce the actual signature (without “`1” definitions for generics) is to completely rebuild it, which becomes very cumbersome.
In the case I have a signature like this in my interface:
ICar Drive<TCar>(Expression<Func<TWheel, bool>> wheels, int miles)
Is there any way to completely reproduce that with reflection without having to disect the entire MethodInfo details, or is there a quick way to get the string above out so I can write it in my T4?
Any help would be greatly appreciated!
When I need to generate code, I often look to the
System.CodeDomnamespace. It lets you build up a logical representation of code and then get the corresponding source code for what you built. However, I don’t know if I can say that this way isn’t also ‘cumbersome’ as you said in your answer (and this certainly involves ‘dissecting’ theMethodInfo. However, it does give you a pretty decent foundation. Just by passing in the interface you want to ‘clone’, the name of the new class and the base class that you want to extend like so:will result in this:
Also, you can change a few characters in the code and switch to the VB provider and you’ll get Visual Basic output (perhaps not useful but kinda cool):
Here is the
GenerateCodebeast. Hopefully the comments can explain what’s going on: