Where would you use C# Runtime Compilation?

大憨熊 提交于 2019-12-05 15:24:04

问题


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.


回答1:


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.




回答2:


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.




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




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/143973/where-would-you-use-c-sharp-runtime-compilation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!