Code generation with .Net Standard

1.7k views Asked by At

I have a little library which relies hugely on Emitting classes and methods.

I want to migrate it to .Net Standard because it doesn't use any unmanaged things so can be easily running on whatever OS. But when I ran Portability checker on my solution, it showed that everything is OK with my Expression generator part, but it is whining on Emit usages.

Here is analysis for net452,netstandard1.6 and netstandard2.0.

enter image description here

My question is if there is some modern and recommended way to generate classes at runtime which is supported by .Net Standard or I can just forget about porting my library to it?

1

There are 1 answers

1
Alex Zhukovskiy On BEST ANSWER

Well, I found that nowadays in .Net Standard we have expression trees to generate standalone delegates and old fashioned Emit (available with System.Reflection.Emit and System.Reflection.Emit.Lightweight namespaces) for the rest. Unfortunly, we lost bridge between the former and the latter (I mean LambdaExpression.CompileToMethod, see question).

So generaly there is almost same code generation power as in full desktop .Net until you don't need to generate types at runtime (implement some interface on the fly, for example). In this case you are forced to emit IL manually.