Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are dynamic methods and how is DynamicMethod different from MethodBuilder?

I've come across dynamic methods a little in reflection-based C# code, and I'm yet to figure out precisely what they are. There specifically seems to be a DynamicMethod class that allows the generation and specification of CLR methods at runtime. But then there's also the MethodBuilder class. They both seem to do very similar things. Apparently "dynamic assemblies" are AssemblyBuilder classes and "dynamic types" are TypeBuilder classes. They all reside in the System.Reflection.Emit namespace in any case.

MSDN seems to have precious little high-level information on this subject. So if someone could explain what dynamic methods are, where exactly all the XYZBuilder classes come into play here, and what they're each used for, that would be great. Any other Reflection.Emit types and functionality I should know about would be appreciated too.

like image 869
Noldorin Avatar asked Mar 18 '12 04:03

Noldorin


People also ask

What are dynamic methods in C#?

In C# 4.0, a new type is introduced that is known as a dynamic type. It is used to avoid the compile-time type checking. The compiler does not check the type of the dynamic type variable at compile time, instead of this, the compiler gets the type at the run time.

What is dynamic method in physics?

The dynamic method is a procedure for the determination of the masses of asteroids. The procedure gets its name from its use of the Newtonian laws of the dynamics, or motion, of asteroids as they move around the Solar System.

What is reflection emit?

Reflection. Emit is a powerful namespace in which we can dynamically emit transient and persisting assemblies at runtime. Reflection. Emit produces a low-level, language-neutral MSIL. Normally, we create an assembly by saving its source code to disk and then compiling that source code.


1 Answers

I think the documentation for DynamicMethod explains this well:

You can use the DynamicMethod class to generate and execute a method at run time, without having to generate a dynamic assembly and a dynamic type to contain the method. The executable code created by the just-in-time (JIT) compiler is reclaimed when the DynamicMethod object is reclaimed. Dynamic methods are the most efficient way to generate and execute small amounts of code.

If you need to dynamically create one or more methods, use DynamicMethod. If you want to create whole types, it means you need to create a dynamic assembly (AssemblyBuilder), then create a module inside it (ModuleBuilder) and then create one or more types (TypeBuilder). To create methods inside those types, you would use MethodBuilder.

Another difference is GC: DynamicMethods can be always garbage collected and they are collected one by one. That is, any method can be collected as soon as you stop using it. Dynamic assemblies, on the other hand, can be collected only when you specify it (by using AssemblyBuilderAccess.RunAndCollect) and they are always collected assembly by assembly. For example, if you have two types in an assembly, and you use only one of them, the other one can't be collected.

like image 52
svick Avatar answered Sep 28 '22 20:09

svick