This is really tricky since T4 patterns and reflection do not play together.
See my answer for a similar, possible duplicate, here:
Create column headers based on the DisplayName attribute?
Update:
I do not need to check my answer, I went this way and found the reflection of + t4 templates problematic. The banal example provided by another respondent does not reflect the true complexity of the task that the questioner is trying to accomplish. Try using a more realistic reflection target, such as native code, rather than a .NET class.
The reason why this is clearly explained in the links I provided, but since itβs easier to vote for me before reading the links, I will post here:
"The obvious first choice for accessing .NET metadata is Reflection. Built into the .NET base class library, this API provides access to .NET type metadata. Unfortunately, Reflection is optimized for code execution. - for code assembly - assembly loaded using Reflection can only be unloaded using AppDomain. Since T4 templates are compiled into .NET assemblies and cached to improve code generation performance, using Reflection to access the component assembly forces T4 to block it. This prevents you from changing ive / recompile the component as long as you do not close or open the solution. While you can certainly live with this disappointment, it would be nice to avoid it. "
source share