9

I've programmed .NET and C# for years now, but have only recently encountered the DynamicMethod type along with the concept of a Dynamic Assembly within the context of reflection. They seem to always be used within IL (runtime code) generation.

Unfortunately MSDN does an extraordinarily poor job of defining what a dynamic assembly/method actuallty is and also what they should be used for. Could anyoen enlighten me here please? Are there anything to do with the DLR? How are they different to static (normal) generation of assemblies and methods at runtime? What should I know about how and when to use them?

Noldorin
  • 144,213
  • 56
  • 264
  • 302
  • There is no type named "DynamicAssembly" in the framework. Are you talking about AssemblyBuilder? The MSDN Reflection.Emit tutorial is here: http://msdn.microsoft.com/en-us/library/3y322t50.aspx – Hans Passant Dec 21 '11 at 16:11
  • @HansPassant: Okay, maybe no type, but definitely a concept: http://msdn.microsoft.com/en-us/library/4xtysk39.aspx – Noldorin Dec 21 '11 at 16:21

1 Answers1

5

DynamicMethod are used to create methods without any new assembly. They also can be created for a class, so you can access it's private members. Finally, the DynamicMethod class will build a delegate you can use to execute the method. For example, in order to access a private field:

var d = new DynamicMethod("my_dynamic_get_" + field.Name, typeof(object), new[] { typeof(object) }, type, true);
var il = d.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld, field);
if (field.FieldType.IsValueType)
    il.Emit(OpCodes.Box, field.FieldType);
else
    il.Emit(OpCodes.Castclass, typeof(object));

il.Emit(OpCodes.Ret);
var @delegate = (Func<object, object>)d.CreateDelegate(typeof(Func<object, object>));

Hope it helps.

Ivo
  • 8,172
  • 5
  • 27
  • 42
  • And it has nothing to do with DLR. It's a method created on the fly. – Ivo Dec 22 '11 at 23:33
  • Ah, I see. That clears it up a good bit, so thanks for that. Is there any equivalent in a dynamic *type* or such? Or what about assembly? I thought I could always create assemblies on the fly in the past, and I don't remember the word "dynamic" coming into it really. – Noldorin Dec 23 '11 at 00:21
  • For types and assemblies, it's just a naming. DynamicMethod is an actual class. – Ivo Dec 23 '11 at 05:04
  • Oh, so one would create a 'dynamic' assembly/type just as one would normally in .NET? The same thing goes on behind the scenes? – Noldorin Dec 23 '11 at 09:45
  • Yeah, using AssempblyBuilder and all that jazz. As you create them on the fly, they call them "dynamic". – Ivo Dec 23 '11 at 15:53
  • Ah, got it. Last thing: have you found yourself using dynamic assemblies/types/methods much in the past (any/all of)? If so, briefly what have been some typical applications? – Noldorin Dec 23 '11 at 20:03
  • I've been working on http://mirrormirror.codeplex.com doing exactly that :) The tipical application is to create fastest reflective access to members, because you use a delegate instead than a reflection call – Ivo Dec 24 '11 at 03:06
  • Exactly what I thought. I'm vaguely aware Dapper uses dynamic methods in its implementation. MirrorMirror looks interesting (good opportunity for you to plug it heh) -- does it do caching of the dynamic methods on each `Get`/`Set` call (for example) though? – Noldorin Dec 24 '11 at 03:11
  • Yeah, it caches all the delegates by the property name. Thanks! – Ivo Dec 24 '11 at 03:13
  • Sounds good. I'll definitely consider using the library in future projects. :-) – Noldorin Dec 24 '11 at 03:27