문제

최근 C# 런타임 컴파일에 관한 다른 사이트에서 최근에 다른 사이트에서 다른 것을 검색하면서 아이디어가 흥미로웠다 고 생각했습니다. 이것을 사용한 적이 있습니까? 나는 이것을 어떻게/언제 사용할 수 있는지, 어떤 문제가 해결되는지 결정하려고 노력하고 있습니다. 나는 당신이 그것을 어떻게 사용했는지 또는 그것이 어떤 맥락에서 의미가 있는지 듣는 데 매우 관심이 있습니다.

고맙습니다.

도움이 되었습니까?

해결책

일반적으로, 나는 당신이 현재 반사를 사용하고 있으며 성능을 최적화 해야하는 경우에 사용됩니다.

예를 들어, 반사를 사용하여 메소드 X를 호출하는 대신 런타임에 동적 메소드를 생성하여이를 수행합니다.

다른 팁

이를 사용하여 응용 프로그램에 스크립팅 지원을 추가 할 수 있습니다. 예를 들어보십시오 여기 또는 여기.

내부 객체 프레임 워크의 일부를 스크립팅 부분에 게시 할 수 있으므로 사무실의 VBA와 동일한 효과를 가진 응용 프로그램에 상대적으로 쉽게 추가 할 수 있습니다.

동적 프록시를 생성 할 때 이것 (런타임 컴파일 / System.Reflection.emit 클래스 사용)을 보았습니다. 코드 프로젝트 샘플 ) 또는 반사 호출을 최적화하는 다른 수단 (시간별).

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.

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.

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.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top