Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real world uses of Reflection.Emit

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:

  • AOP (cache injection)
  • interface list wrapping
  • compare objects

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).


Dynamically generating a mock object which implements some interface. Example frameworks which do this: moq, rhino mocks.


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