views:

2048

answers:

7

I happened upon a brief discussion recently on another site about C# runtime compilation recently while searching for something else and thought the idea was interesting. Have you ever used this? I'm trying to determine how/when one might use this and what problem it solves. I'd be very interested in hearing how you've used it or in what context it makes sense.

Thanks much.

+5  A: 

Typically, I see this used in cases where you are currently using Reflection and need to optimize for performance.

For example, instead of using reflection to call method X, you generate a Dynamic Method at runtime to do this for you.

therealhoff
Thanks for the response!
itsmatt
+1  A: 

At least one case you might use it is when generating dynamic code. For example, the framework is using this internally to generate XML serializers on the fly. After looking into a class at runtime, it can generate the code to serialize / deserialize the class. It then compiles that code and users it as needed. In the same way you can generate code to handle arbitrary DB tables etc. and then compile and load the generated assembly.

Ron Harlev
this is a good example of where the tradeoff is a serializer based on reflection or a dynamically generated serializer
therealhoff
+1  A: 

Well, all C# code is run-time compiled, since it's a JIT (just-in-time) compiler. I assume you are referring to Reflection.Emit to create classes etc. on the fly. Here's an example I have seen recently in the Xml-Rpc.Net library.

I create a C# interface that has the same signature as an XML-RPC service's method calls, e.g.

IMyProxy : IXmlRpcProxy
{
    [XmlRpcMethod]
    int Add(int a, int b);
}

Then in my code I call something like

IMyProxy proxy = (IMyProxy)XmlRcpFactory.Create(typeof(IMyProxy));

This uses run-time code generation to create a fully functional proxy for me, so I can use it like this:

int result = proxy.Add(1, 2);

This then handles the XML-RPC call for me. Pretty cool.

Matt Howells
That's interesting and helps clarify it a bit for me. Thanks, Matt.
itsmatt
+1  A: 

I used runtime compiler services from .NET in my diploma thesis. Basically, it was about visually creating some graphical component for a process visualization, which is generated as C# code, compiled into an assembly and can then be used on the target system without being interpreted, to make it faster and more compact. And, as a bonus, the generated images could be packaged into the very same assembly as resources.

The other use of that was in Java. I had an application that had to plot a potentially expensive function using some numerical algorithm (was back at university) the user could enter. I put the entered function into a class, compiled and loaded it and it was then available for relatively fast execution.

So, these are my two experiences where runtime code generation was a good thing.

OregonGhost
+1  A: 

something I used it for was for allowing C# and VB code to bu run by the user ad-hoc. They could type in a line of code (or a couple lines) and it would be compiled, loaded into an app domain, and executed, and then unloaded. This probably isnt the best example of its usage, but an example of it none-the-less

mattlant
Holy cow! Please tell me you did that just to prove you could. :)
MusiGenesis
Heh. Kinda funny, but d2botnet, it was a .net botting engine for diablo 2. People wrote bots in .net to automate the game. I also allowed from the chat dialog to be able to type in: exec cs SOMECODE, or exec vb SOMECODE. for example, to get their x,y exec cs Game.Print(Me.X + " + Me.Y); ...
mattlant
But you could actually get creative and put whatever you wanted into it. I'll have to pull up the source to grab the actual code to do it it anyone wants.
mattlant
+3  A: 

You can use this to add scripting support to your application. For examples look here or here.

It is quite easily possible to publish parts of your internal object framework to the scripting part, so you could with relative ease add something to your application that has the same effect as for example VBA for Office.

TToni
Thanks for the info and the links!
itsmatt
+2  A: 

I've seen this (runtime compilation / use of System.Reflection.Emit classes) in generating dynamic proxies ( Code project sample ) or other means of optimizing reflection calls (time-wise).

Andrei Rinea
Thanks for the link, Andrei! Appreciate the link.
itsmatt
You're welcome :)
Andrei Rinea