Visual Studio 2010 T4 Code Generation for Templated Types

I am using the default T4 templates to generate code based on my UML Model. I have a number of classes that expose Template Parameters. These generate code as expected. For example the MessageResponse class exposes a Parameter of TMessageType. This generates the code as expected:

public class MessageResponse<TMessageType>


The problem comes in when I bind this parameter to another class. For example I create a class called AccountMessageResponse which binds the Template Parameter to a class call Account. What I expect to see is a generated class as follows:

public class AccountMessageResponse : MessageResponse<Account>


However, no code is generated. When I look at the output log for the code generation I see this message:

'RestModels::AccountMessageResponse' - (Class): no file generated because the 'AppliesToElement' method of the template 'ServiceClassTemplate.t4' evaluated to false.

Is code generation for templated types not supported in the current default templates? Is this something I need to do myself, or am I doing something wrong?



It seems the problem occurs when you combine templated types with an inheritance relationship. Here is a workaround. To achieve the results required all you need to do is:

1) Add a template parameter, TMessageType, to your class RequestMessage. This will generate the following:

public class RequestMessage<TMessageType>

2) Create a class called AccountRequestMessage.

3) Set the TemplateBinding parameter for AccountRequestMessage to 'RequestMessage'. Then set the TemplateBinding class to 'Account'. This will then generate the following wherever you have referenced AccountRequestMessage. For example if you have an operation Foo which returns AccountRequestMessage the following code will be generated:

public RequestMessage<Account> Foo()

4) If you need to generate an actual class you then need to add a new class AccountRequestMessageImpl which inherits from AccountRequestMessage. This will generate:

public AccountRequestMessageImpl : RequestMessage<Account>

This is a little counter intuitive but it is simpler than trying to modify the T4 templates.


Need Your Help

Dynamically adding controls of unknown type and calling methods on control


I am putting together a website that grabs content from a database and fills it into a component which is then loaded onto the page. The catch is that there are many different components which are

exclude match based on node list

xml xslt xslt-1.0

First question on here so bear with me...

How to deserialize JSON text into a date type using Windows 8 JSON.parse?

javascript json windows-8

I'm building a Windows 8 Metro app (aka "Modern UI Style" or "Windows Store app") in HTML5/JavaScript consuming JSON Web Services and I'm bumping into the following issue: in which format should my...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.