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

975 views Asked by At

Now I programmatically generate sources and create some classes before compilation and obviously add it to project in solution. Maybe it is possible to "silently" add classes before compilation without creating .cs files in disk and not showing these classes in Solution Explorer (maybe using Roslyn).

EDIT: I must not use any runtime code generation.

3

There are 3 answers

0
Jan lul On

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

Don't forget to add a reference to the DLL (class library) in your main project.

0
Matthew Abbott On

You could probably do something with MSBuild, creating a custom project target which does the work, but I've never done this.

What I have done recently which is now achievable on the DNX-based ASP.NET 5 platform, is a concept known as meta-programming. I've written a blog article about this concept specifically with examples of generating code at compile time. In my particular example, I've got a class that won't compile, but then with an introduction of an ICompileModule, I can fill in the missing method return statement 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 running these instances before your main project compilation. This enables you to add/remove/replace syntax trees in the compilation before the compiler finishes its work.

If you're looking to develop on ASP.NET 5, this could enable you to do what you need, but I don't know how you would go about doing this otherwise.

Seems quite aspecty to me.

I asked a question which I also answered myself about engineering a compile-time solution that performs code generation for another scenario:

Getting interface implementations in referenced assemblies with Roslyn

And lastly, other examples where this might be useful, and something I've been toying around with, is the ability to generate EF-style migration classes from .sql files embedded in my assemblies. All these scenarios are now easier for me to implement on ASP.NET 5 + Roslyn.

0
Greg Trevellick On

Without knowing your use-case properly, here's an idea...

Create a VSIX that listens to an 'on build' event

Upon initialisation of the build, the VSIX creates your new classes*

The same VSIX will also listen for a 'build complete' event

Upon completion of the build the VSIX would tear down the new classes

*Your question states that the classes should not be created on disc, so the VSIX could

  • create the classes as a memory stream (?)

  • add the new class as code within existing files on disc

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

  • the new class could 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 would need to be auto-edited (by the vsix) to reference the new file(s). Presumably you want the project file reverted aferwards ?

And if, unlike me, you want to get down and dirty, you could always interfere with the IL, but you're on your own there !

As TarkaDaal says, without knowing why you need this it's not easy to provide a more definative answer.