3

Is there anything similar to Reflection.Emit.DynamicMethod in Cecil? Thanks.

  1. DynamicMethod

Edit:

What about for the following things?

  1. EmitCall (e.g.
    IL.EmitCall(OpCodes.Callvirt, GetBuildKey, null); IL.Emit(OpCodes.Unbox_Any, dependencyType); )
  2. LocalBuilder (e.g. LocalBuilder resolving = ilContext.IL.DeclareLocal(typeof(bool));)
  3. System.Reflection.Emit.Label (e.g. Label existingObjectNotNull = buildContext.IL.DefineLabel();) //Do I have to use TextMap?
  4. ILGenerator.BeginCatchBlock (e.g. ilContext.IL.BeginCatchBlock(typeof(Exception)); )
  5. ILGenerator.MarkLabel (e.g. ilContext.IL.MarkLabel(parameterResolveFailed); )
  6. ILGenerator.EndExceptionBlock() (e.g. ilContext.IL.EndExceptionBlock(); )
Michael Sync
  • 4,834
  • 10
  • 40
  • 58
  • Is cecil mono library, right? I have added a tag *mono* – YOU Apr 17 '10 at 03:08
  • @Michael, you moving from .Net to mono or just testing it out? Oh, btw, I think we know each others before. – YOU Apr 17 '10 at 03:35
  • I'm just testing and reading some codes of Cecil. I have a few friends called "Mark".. maybe, you are one of my friends "Mark" that i know. :) Are you from China? or SG? But SG doesn't ban Google. :) Give me some hints. man.. :) – Michael Sync Apr 17 '10 at 03:49
  • @Michael, you recently posted about Silverlight user meet up on MITP, right? search "Mark" there, top result is me. – YOU Apr 17 '10 at 04:52
  • oh! okay.. I know you.. we had a few conversions before in other forum.. cool. man. good to see you here.. – Michael Sync Apr 17 '10 at 09:30
  • maybe.. EmitCall = callWriteLine = worker.Create(OpCodes.Call, writeLine); but for Label, I think i need to create an instruction.. – Michael Sync Apr 17 '10 at 17:41
  • seems like noone are not that familiar with Cecil. – Michael Sync Apr 19 '10 at 07:50

1 Answers1

5

There's no way to create a DynamicMethod with Cecil, nor does it have an equivalent.

A DynamicMethod is strongly tied to the runtime, while Cecil is completely decoupled. The two of them have a completely separate type system. DynamicMethod are meant to be, well, dynamic, and as such have to use the System.Reflection type system, as it's the one available at runtime. Mono.Cecil has another representation of this type system suitable to static analysis, without having to actually load the assembly at runtime. So if you want to use a DynamicMethod, you have to use it along with its environment.

This question was originally asked, iirc, in the context of runtimes without DynamicMethods or SRE all-together, like the Compact Framework, where Cecil can be used to emit code at runtime.

Of course it's possible, but then you have to pay the price of loading the assembly, which is no small price on CF devices. It means that if you could somehow emulate a DynamicMethod by creating an assembly with only one static method with Cecil, it sounds a terrible idea. The assemblies would not be collectable (DynamicMethods are), making it a giant memory leak.

If you need to emit code at runtime on the Compact Framework, emit as less as possible, and emit as few assemblies as possible.

Jb Evain
  • 17,319
  • 2
  • 67
  • 67