Dynamic Assemblies and Methods
Asked Answered
D

1

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?

Decreasing answered 21/12, 2011 at 15:15 Comment(2)
There is no type named "DynamicAssembly" in the framework. Are you talking about AssemblyBuilder? The MSDN Reflection.Emit tutorial is here: msdn.microsoft.com/en-us/library/3y322t50.aspxCowherd
@HansPassant: Okay, maybe no type, but definitely a concept: msdn.microsoft.com/en-us/library/4xtysk39.aspxDecreasing
A
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.

Abran answered 22/12, 2011 at 23:32 Comment(10)
And it has nothing to do with DLR. It's a method created on the fly.Abran
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.Decreasing
For types and assemblies, it's just a naming. DynamicMethod is an actual class.Abran
Oh, so one would create a 'dynamic' assembly/type just as one would normally in .NET? The same thing goes on behind the scenes?Decreasing
Yeah, using AssempblyBuilder and all that jazz. As you create them on the fly, they call them "dynamic".Abran
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?Decreasing
I've been working on 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 callAbran
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?Decreasing
Yeah, it caches all the delegates by the property name. Thanks!Abran
Sounds good. I'll definitely consider using the library in future projects. :-)Decreasing

© 2022 - 2024 — McMap. All rights reserved.