Real world uses of Reflection.Emit

Viewed 20870

In all the books I've read on reflection they often say that there aren't many cases where you want to generate IL on the fly, but they don't give any examples of where it does make sense.

After seeing Reflection.Emit as a job requirement for a gaming company I was curious where else it's being used.

I'm now wondering if there are any situations you've seen in the real world were it was the best solution to the problem. Perhaps it is used as an implementation to a design pattern?

Note I imagine PostSharp / AOP uses it.

17 Answers

Expression.Compile essentially does this - that is key to some of LINQ.

I am currently using reflection emit to re-write a serialization API - because sometimes reflection just isn't good enough. As it happens this will also allow it to generate dlls (much like how sgen works), allowing fully static code (I'm hopeful this will make it iPhone friendly).

I also use a similar approach in HyperDescriptor to provide very fast name-based property-lookup.

I've also used emit to do things like:

all related to SO questions.

Finally, this IL approach is the core of protobuf-net "v2"; the reason here is that it allows me both to have a fast model at runtime (compiling it via IL on the fly), and to write the same directly to a static-compiled dll, so that it works on things like iPhone, Phone 7, etc (which lack the necessary meta-programming APIs).

I'm using it as a way to create dynamic proxies on the fly to wrap a class. NHibernate uses this same pattern to proxy calls to POCO objects to instead query a database.

Any time you want to be able to "write code" (i.e. create a new function, etc.) dynamically, you'll need Emit.

Castle DynamicProxy uses it for, you guess, dynamic proxies. DynamicProxy is then used by the Castle's IoC container Windsor and OR mapper ActiveRecord.

The DLR and DLR related languages heavily rely on Reflection.Emit

I remember seeing Relection.Emit used in chapter 8: "On-the-Fly Code Generation for Image Processing" of Beautiful Code. Basicly the author specializes a function for doing a certain set of image processing operations on a given image, which in turn leads to greatly reduced execution time.

Mocking libraries also use Reflection.Emit to generate proxies used in Unit Testing.

  1. Generate declarative code, such as using interface to declare underlying HTTP REST service. https://github.com/neurospeech/retro-core-fit

  2. Performance booster, most of the time I use Expression.Compile to create a fragment of code to retrieve information quickly by compiling giving expression to compiled delegate which can be executed in future. If you use PropertyInfo.GetValue, it is very slow, but if you create an expression to access property and compile it to a delegate (which internally uses Reflection.Emit), it is huge savings on CPU time.

I like to explore more for AI-Inspired for self-learning. It allows us to create a class or module at run-time

Generating enums from external data:

Dynamic enum in C#

In that example the data comes from a lookup database and the enum is created at compile-time by reading out of the database. This maintains type-safety without having to manually maintain duplicate sources of information.

I'm actually developing my own language and I'm building its compiler using .NET Framework's Reflection.Emit.
Someone could think that building a compiler using C# is not good, but things are actually working fine and compile-time performances are good enough. Also, this is obviously not related at all to the runtime performances, which will depend upon CLR, not my compiler (except for minimal optimization like operator precedence). I'll publish it on GitHub as soon as I'll have completed it.

Related