0

I'm trying to figure out if we could generate code with a T4 Template file post-build.

In fact, I'm kinda new to c# and I needed to find a solution where I generate class from Xml file that could be edited by the user at anytime and the first solution that come to me is T4 Template. Then I render those generated classes on a treeView and the according properties on a property grid.

Now, I think that it is only generate code at pre-build time, but I'm not sure about that since I don't really see a lot of stuff about that on the internet. I'm still pretty sure now that it's not possible.

So, a second problem comes to me : I finished my entire project like that, so if you all have an idea or another solution that could be nice, i'll really appreciate that.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • You can use T4 at runtime *but* that's not necessary if all you want to do is display XML data. Have you tried deserializing the XML into an XmlDocument and displaying it? – Panagiotis Kanavos Feb 15 '22 at 09:07
  • 1
    As for `cut my hey everyone everytime I want to edit` that's because SO isn't a discussion forum, it's a Q&A site. `Hey everyone` only adds noise and makes it harder to read the question – Panagiotis Kanavos Feb 15 '22 at 09:08
  • @PanagiotisKanavos I don't really understand the definition or concept of deserializing, but I used a library to read the XmlDocument and I don't really succeed to figure out how to display it on a treeview/propertygrid (cause my boss really want me to use these) – Tony Prime Feb 15 '22 at 09:23
  • The boss wants to use an Explorer/Properties view, similar to Visual Studio or perhaps, Outlook. Developers like this because they don't have to create forms. Users *hate* this because the display is an unorganized list of properties, without grouping or visual cues. So do developers, when they have to use other people's PropertyGrid UIs. – Panagiotis Kanavos Feb 15 '22 at 09:30
  • A `PropertyGrid` works with anything that has a `TypeConverter`, not just classes. Using a TypeConverter is primarily a way to customize the PropertyGrid, but can also be used to adopt arbitrary types for use with PropertyGrid. [This probably duplicate question](https://stackoverflow.com/questions/4591115/how-to-load-xml-document-in-property-grid) shows how to do this. There are other similar SO questions too. Once you load an XML file into an `XmlDocument` or `XDocument` you can use a TypeConverter to display it in a PropertyGrid – Panagiotis Kanavos Feb 15 '22 at 09:31
  • You should also consider loading the data into an XmlDataDocument and treating the data as a DataSet with DataTables. A DataTable can be bound to an editable DataGrid. This UI is far better when an element contains an array of other elements – Panagiotis Kanavos Feb 15 '22 at 09:36

1 Answers1

0

The 'T4Executer' extension has an option to attach the execution of each T4 template to build events. Before, during and after build, or not run them at all.

Doesn't work for Visual Studio 2022 yet though.

Tim Maes
  • 473
  • 3
  • 15