Where would you use C# Runtime Compilation?
Asked Answered
K

7

11

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.

Kaceykachina answered 27/9, 2008 at 16:42 Comment(0)
T
7

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.

Triggerfish answered 27/9, 2008 at 16:46 Comment(1)
If you want to do dynamic things like EVAL() etc, then we might need the concept of runtime compilationIllconditioned
L
5

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.

Leoraleos answered 27/9, 2008 at 18:56 Comment(0)
A
3

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

Agglomerate answered 28/9, 2008 at 17:24 Comment(0)
M
2

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.

Mitchum answered 27/9, 2008 at 16:49 Comment(1)
this is a good example of where the tradeoff is a serializer based on reflection or a dynamically generated serializerTriggerfish
C
2

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.

Collage answered 27/9, 2008 at 16:52 Comment(1)
That's interesting and helps clarify it a bit for me. Thanks, Matt.Kaceykachina
P
2

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.

Puccini answered 27/9, 2008 at 17:12 Comment(0)
S
2

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

Severn answered 27/9, 2008 at 17:18 Comment(3)
Holy cow! Please tell me you did that just to prove you could. :)Shotton
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); ...Severn
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.Severn

© 2022 - 2024 — McMap. All rights reserved.