C # How to generate code from code

Is it possible to generate and build some C # code based on code from the same project. I tried with T4 and Reflection, but there are some problems with blocking the assembly. Is there another way?

+6
c # code-generation
source share
5 answers

Reflection works great for me. You can work around build blocking problems by isolating the build task to a separate AppDomain in VS. When the task completes, any assemblies that need to be used to generate the code will be unloaded along with the AppDomain task. See AppDomainIsolatedTask .

+1
source share

You can definitely write your own code generator, everything in C # - after all, the "code" that is created is just a text file that you write out.

But what is wrong with the T4 templates? They offer a lot of functionality that you do not need to reinvent - why not use it? Could you tell us more about the problems with T4?

T4 is really just a bunch of classes in .NET, so you can definitely write your own code generator that handles part of the logic, and use T4 to create patterns and replace those parts of the patterns. But then again: to help you diagnose your problems with T4, we will need to learn more about these ...

+1
source share

This example from Oleg Sych uses the FXCop introspection mechanism instead of reflection. Thus, assemblies are not blocked.

Unfortunately, Reflection is optimized for code execution. One specific limitation makes it unsuitable for code generation - assembly loaded using Reflection can only be unloaded from its AppDomain. Because T4 templates are compiled in .NET. assemblies and caching to improve code generation performance, using Reflection to access the assembly component causes a T4 lock.

Alternatively, if you are only targeting Linq classes to SQL, you can generate code from the dbml file instead of the code that L2S generates from dbml. I have an example of something like this (edmx file) on my own blog .

+1
source share

There is a third-party version of C # .NET.NET JavaCC that we use at work.

Also an interesting article on how to create it: http://msdn.microsoft.com/en-us/magazine/cc136756.aspx

0
source share

It really depends on what exactly you are trying to achieve, but in general I would recommend using T4 templates.

And yes, you can use T4 templates inside your project to generate code in your project based on some local settings, but you must determine what you are trying to do.

If you want to generate code based on some classes that you define in one project, this does not seem to be something easily achievable (because you want to compile some of the classes in the current project, create code based on them and then generate classes ... umm ..?)

But if you want to save some settings, then run the T4 template and create code based on these settings - this is easily achievable. T4MVC is an example (they generate code based on the settings file, which is copied and saved in the project along with the T4 template). This template also considers the current files available in the solution and generates string constants based on each file. Sounds like this will really help you solve your problem, whatever that is :)

If you are still not sure, you can provide more detailed information about what you want to do, and we will try to help you :)

0
source share

All Articles