Can we build an assembly from both CodeExpressions and literal source code at the same time?

I have a situation where one part of my code is generated through CodeExpressions and the other by the user himself (as in: the user simply writes his code, as usual, which I would take and add to my assembly when it compiles).

Is it possible to create an assembly containing both of these elements? Caution: these two approaches will contain partial classes, so they must be in the same assembly.

Another approach that I had in mind was perhaps translating both of these lines into a string representation, and then creating an assembly from that line, but I doubt that I can get the source code from the type generated by the user (at compile time).

While working on this idea, I can write the code generated by CodeExpressions into a text file and combine it with .cs files. The timeline will look like this:

  • The user wrote his classes
  • CodeDom tree is installed programmatically
  • User creates a project.
  • CodeDom generates source text to text file
  • The program reads the contents of user-defined .cs files
  • The program reads the generated text file
  • Program combines these two
  • The program creates a new .dll from the combined source code

I could skip the (redundant) steps of writing and reading my generated CodeDom source to a text file and just writing it to memory, of course. In fact, the easiest way is to use Pre-Processed T4 templates and load the results of these templates into memory and compile the assembly from this line.

As you can see, this is very dirty, but now it looks the most doable. I looked through all the options that could make this easier?

Background :

I am creating a library that will create an assembly with classes that are user defined. The way this works is in the following order:

  • User links to my library in his project
  • User creates a new instance of TinyTypeSetup
  • User adds Tiny Type definitions to it.
  • The user runs the program
  • The program generates an assembly of the specified types through CodeDom

What I'm trying to add now is the ability for the user to create their own source files and immediately add these files to the assembly that is being generated. This will allow the user to specify partial classes with their own methods, on top of those that I generate myself.

+7
c # .net-assembly code-generation codedom
source share
2 answers

You are not talking about a scenario where CodeExpression is always useful. This is a source code generator, a certain type of source code that is generated is determined by the provider you choose.

But by no means does the user of your project really care about this language in your intended use. He never looks at him; he never compiles it himself. Only you do not care, you need to choose the right CodeCompiler. The user only selects the assembly that he produces. And under the .NET conventions, the language that was used to create the assembly never matters. The metadata inside the assembly is fully linguistic-agnostic.

CodeObjects are useful in a scenario where the source code is automatically generated and added to the user's project. To compile later when the user creates his project. Good examples are the various designers built into Visual Studio, such as the resource constructor and the Winforms designer. If necessary, they must generate code appropriate for the type of user project.

This should be avoided if you do not need it. The biggest hang with CodeDom code generators is that it is able to generate a subset of statements that are valid in the language. And of course it's ugly to use; it puts your code. You only need to generate text, the language you choose does not matter. Since you seem to favor C #, this is the text you should generate.

Consider a larger solution. This will probably be much better if the user can actually open their own designer inside Visual Studio itself. Therefore, this additional step, namely that the translator is no longer needed to switch from TinyType to the assembly. Easy to use, IntelliSense highlights. Now it makes sense to generate code. It takes a lot of work, creating designers is not so simple. Watch the ball, no one will like to generate this XML file that you need. Creating tiny types in C # is already easy, it just doesn't need a lot of help. In any case, the user creating his own types from your tiny types also does not need any help. VS already supports it directly.

+5
source share

Although there is a bit of a learning curve. But, of course, better than dealing with a string. A look at the .NET Compiler Platform (Roslyn) for potential customers can help. Review start with.

It provides API functions for semantic analysis and transformation. Diagnostics and correction of the code.

0
source share

All Articles