5

are there any classes in the .net framework that allow me to generate classes that i can save as .cs or .vb files ?

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.