Where would you use C# Runtime Compilation?

前端 未结 7 1236
余生分开走
余生分开走 2021-02-20 05:34

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

相关标签:
7条回答
  • 2021-02-20 06:01

    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.

    0 讨论(0)
  • 2021-02-20 06:06

    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

    0 讨论(0)
  • 2021-02-20 06:08

    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.

    0 讨论(0)
  • 2021-02-20 06:13

    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.

    0 讨论(0)
  • 2021-02-20 06:19

    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.

    0 讨论(0)
  • 2021-02-20 06:20

    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.

    0 讨论(0)
提交回复
热议问题