are there any classes in the .net framework that allow me to generate classes that i can save as .cs or .vb files ?
C# Source Generators is a Roslyn compiler feature introduced in C#9/. NET 5. It lets C# developers inspect user code and generate new C# source files that can be added to a compilation.
A code generator is a tool or resource that generates a particular sort of code or computer programming language.
Source-code generation is the process of generating source code based on a description of the problem or an ontological model such as a template and is accomplished with a programming tool such as a template processor or an integrated development environment (IDE).
Do you really need specific .NET classes, or can you live with an addin to Visual Studio??
If you have Visual Studio, definitely check out the T4 template - one of the least known and yet most valuable assets in VS!
There's at least a command-line tool that you could call from .NET code to do the transformation, but I'm pretty sure you could also invoke some transformation engine as a .NET class and have it generate C# or VB.NET code from your T4 template programmatically.
Typically, using T4 is a lot easier than CodeDom - it has its limits, but for the most part, it's a great technology to get things done fast and easy.
See the System.CodeDom namespace. Basically you use CodeDom to create an abstract model of the class. Then you instantiate a CSharpCodeProvider or VBCodeProvider and use one of the CodeDomProvider.GenerateCodeFrom... methods to emit it as the appropriate kind of source code. This is the underlying technology used by the Windows Forms Designer, xsd.exe, etc.
Be warned, however, that CodeDom is quite verbose and fiddly to work with. If your classes a moderately complex, you may want to use a templating engine such as T4 or NVelocity instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With