How to get T4 in VS2010 to iterate over class properties

I use Visual Studio 2010 and install the parent T4EditorPlusModellingTools.

I just play with the T4, never touching it before. What I would like to do is look at one of my classes in the project and write out each of the properties.

Can someone give me absolute advice for beginners on how the .tt file should be structured? I searched this and did not find an answer, so any help would be appreciated.

Thanks.

+2
source share
3 answers

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. "

+3
source

Use Reflection in tags.

Sort of:

<#foreach (PropertyInfo info in myObject.GetType().GetProperties()){#> // do what you want with or any other property of info <#=info.Name#> <#}#> 

try here tuturials msdn

 <#@ template debug="false" language="C#" #> <#@ output extension=".txt" #> <#@ assembly name="System.Xml"#> <#@ import namespace="System.Xml" #> <#@ import namespace="System.Reflection" #> <# XmlDocument doc = new XmlDocument(); foreach (PropertyInfo info in doc.GetType().GetProperties()){#> <#=info.Name#> <#}#> 

print all xmldocument properties

+6
source

If the assembly you are thinking about is part of your project (as the mentioned initial raiser question), then you will see the lock as indicated.

There is a temporary solution to the Codeplex project by Oleg Sych T4Toolbox ( http://t4toolbox.codeplex.com/ ), which introduces the new VolatileAssembly directive, which copies the assembly before downloading it.

We are currently working on fixing this in the main T4 engine as soon as possible.

However, if you want to work with the code in your project, you may not need to process the compiled assembly, but the source code on disk, in which case the CodeModel inside VS, is another option (if you can live with templates that run only inside VS during development).

Here is an example of this at http://www.olegsych.com/2008/07/t4-template-for-generating-sql-view-from-csharp-enumeration/

+4
source

Source: https://habr.com/ru/post/910786/


All Articles