Where would you use C# Runtime Compilation?

2019-04-05 03:14发布

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.

7条回答
虎瘦雄心在
2楼-- · 2019-04-05 03:15

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.

查看更多
家丑人穷心不美
3楼-- · 2019-04-05 03:16

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

查看更多
迷人小祖宗
4楼-- · 2019-04-05 03:21

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.

查看更多
Ridiculous、
5楼-- · 2019-04-05 03:23

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.

查看更多
\"骚年 ilove
6楼-- · 2019-04-05 03:25

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.

查看更多
爷、活的狠高调
7楼-- · 2019-04-05 03:26

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.

查看更多
登录 后发表回答