Luka Horvat
Luka Horvat

Reputation: 4402

T4 template as a part of a library (DLL) that generates classes

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.

Upvotes: 0

Views: 843

Answers (1)

Dmytro Rudenko
Dmytro Rudenko

Reputation: 2524

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.

Upvotes: 1

Related Questions