0

I want to write a template that generates classes based on some attributes of some properties on the already exsisting classes.

I'm pretty sure T4 templates let me do that, but what I want to do is to have those attributes defined in my class library along with the T4 template so that when you reference my DLL and use the attributes in your code, the classes still get automatically generated (it doesn't matter if they are generated as a part of the DLLs assembly, which might not even be possible, or as a part of the assembly that's referencing my DLL)

What I can do is use precompiled templates (or just some function that generates class code as a string) and then make a template in the project that references my DLL and just make a call to the generator function from inside the template. Why I don't want to do this, is because it forces the user to manually create that template in their project.

Luka Horvat
  • 4,283
  • 3
  • 30
  • 48

1 Answers1

1

YOu may create T4 template with TextTemplatingFilePreprocessor Generator. in this case your T4 file will generate C# class with public method TransformText() and another person will call this method for generating files. Generated class is a partial class, so you would add some parameters to it as properties for customizing generation process too.

Dmytro Rudenko
  • 2,524
  • 2
  • 13
  • 22
  • Well I might as well just make my own class generating method then. In this case, as far as I'm aware, templates only help me generate those strings. They do nothing in terms of actually making the classes. I'm looking for a way to generate the code for a class AND automatically add that class to the build process without the user of my library having to deal with ANY templates manually. – Luka Horvat Nov 19 '13 at 14:00
  • In this case simpler way is just pass your tt file to other developers (like MS does with its latest EF6). Otherwise you have to create your own VS extension. This is possible, but extremely harder way. – Dmytro Rudenko Nov 19 '13 at 14:09
  • If there is no other way, then I guess it's what I'll have to do. – Luka Horvat Nov 19 '13 at 14:14