tags:

views:

105

answers:

1

Hello guys,

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(); )
+2  A: 

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
Thanks, Jb.I appreciate it.
Michael Sync