Is it possible to add C # classes just before compilation in Visual Studio

Now I programmatically generate sources and create some classes before compiling and explicitly add them to the project in the solution. Perhaps you can silently add classes before compilation without creating .cs files on disk and not display these classes in Solution Explorer (possibly using Roslyn).

EDIT: I should not use runtime code generation.

+5
source share
3 answers

You can put classes in a separate DLL (class library). When you create this DLL using another solution, you will not see the classes in your solution browser of the project in which you include them.

Remember to add the link to the DLL (class library) in your main project.

0
source

Perhaps you could do something with MSBuild by creating a custom project goal that does the job, but I never did.

What I did recently, which is now available on the ASP.NET 5 platform based on DNX, is a concept known as meta-programming. I wrote an article in a blog article about this concept specifically with examples of code generation at compilation time. In my specific example, I have a class that will not compile, but then with the introduction of ICompileModule I can populate the missing return at compile time.

This is possible because in DNX-based applications, the RoslynCompiler class actually supports loading instances of ICompileModule at compile time, and then starts those instances before your main project compilation. This allows you to add / remove / replace syntax trees in the compilation before the compiler finishes its work.

If you plan to develop on ASP.NET 5, this may allow you to do what you need, but I do not know how you will do it.

It seems quite ascetic for me.

I asked a question in which I also answered myself about the development of a compilation solution that performs code generation for another scenario:

Getting interface implementations in reference assemblies with Roslyn

And finally, other examples where this can be useful, and with something I worked with, is the ability to create migration classes of the EF class from .sql files embedded in my assemblies. All of these scenarios are now easier to implement on ASP.NET 5 + Roslyn.

0
source

Not knowing your use case correctly, here is the idea ...

Create a VSIX that listens for the 'on build' event

After the assembly is initialized, VSIX creates new classes *

The same VSIX will also listen for the 'build complete' event.

When the build is complete, VSIX will change the new classes.

* Your question indicates that classes should not be created on disk, so VSIX may

  • create classes as a memory stream (?)

  • add a new class as code to existing files on disk

  • create a new class as a new file on disk (or cloud?) in C: \ Temp or elsewhere

  • the new class can be part of a partial class (either a real partial class in your application, or an empty new dummy partial class)

In any case, the project file should be automatically edited (vsix) to link to the new file (s). Presumably you want the project file to go back?

And if, unlike me, you want to go down and dirty, you can always interfere with IL, but you yourself are there!

As TarkaDaal says, not knowing why you need it, it's not easy to give a more decisive answer.

0
source

All Articles