Reflection Emit, what’s the point?

Asked

Viewed 148 times

4

Passing a code to MVC, I saw there was a Label label= new Label(); that was underlined with red, when clicking to see the suggestions, I saw the following:

using System.Web.Ui.WebControls

using System.Reflection.Emit 

It hit me the doubt, what is this Reflection Emit?

I saw some things on the net, mainly on the MS site, but I could not understand the use of it.

1 answer

6


It is a rarely used resource. "normal" programmer will probably never use it.

Essentially it serves to create an CIL. That is, it generates the "machine code" of the .NET. virtual machine. It is a simplified way to create a low-level code. He cares to assemble the proper format so that a machine code is understood by . NET.

This way it is possible to create arbitrary code at runtime to be written to a DLL (a file Assembly) or to be executed shortly thereafter. Whether to create some functionality that can only be mounted at runtime, or to achieve greater optimization.

  • The LINQ, for example, makes much use of it to assemble your expression trees.
  • Serialization also uses it.
  • Object creation proxy uses this resource.
  • Some situations the language does not support some resource that can only be achieved by issuing low-level code over high-level language.
  • Dapper is one of the numerous Orms that use this broadcast.

Its use is intense in a compiler or some tool (AOP and mockers, are examples) amending the bytecode of the application developed for . NET (or any other compatible implementation, such as Mono, of course).

Now it is even less necessary. In most cases it is more interesting to use the services of the new compiler called .NET Compiler Platform (former Roslyn project) available at C# 6/Visual Studio 2015. Runtime code generation is much easier and powerful in most cases.

Official source of information.

Reference of namespace.

  • Now I understand! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.