4

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?

6
  • That would be Roslyn, the recommended way to generate classes in C# 6 and above. C# scripting uses it to do just that, generate and run classes from the script text Commented Dec 30, 2016 at 14:37
  • This could be considered a duplicate of this Commented Dec 30, 2016 at 14:38
  • @PanagiotisKanavos It seems that this functionality is under construction so Rosylin cannot generate code in build time atm. Commented Dec 30, 2016 at 14:45
  • Beware that while generating classes at runtime may be done, loading those dynamically generated classes is fundamentally incompatible with some of the platforms that do support .NET Standard. Realistically, if you're serious about .NET Standard, I don't see any option but to radically re-think your entire project, sorry. Just .NET Core could perhaps be possible though, if that might be good enough for you. Commented Dec 30, 2016 at 14:45
  • 1
    Emit is not supported on Xamarin.iOS and that's probably why it does not list in .NET Standard. You will have to check what else can be used, as there is at least a C# IDE on iOS which shows the feasibility. Commented Dec 30, 2016 at 15:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm working on a Roslyn-based solution that works for all frameworks and in compile-time. It's even more powerful. So, if you have to generate expressions just use them. For class-generation it's easier to take Roslyn API and create a plugin that generates them during build. See this project (not mine, i'm just using it) for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.