문제

응용 프로그램 안에서 JavaScript/Python/Ruby를 실행하고 싶습니다.

사용자의 정의를 기반으로 프로세스를 자동으로 작성하는 응용 프로그램이 있습니다. 프로세스는 C#에서 생성됩니다. 고급 사용자가 사전 정의 된 위치에 스크립트를 주입 할 수 있도록하고 싶습니다. 이러한 스크립트는 C# 프로세스에서 실행해야합니다. 예를 들어, 생성 된 프로세스에는 일부 활동이 있으며 중간에 스크립트를 실행하고 프로세스는 이전과 같이 계속됩니다. C#에서 해당 스크립트를 실행하는 메커니즘이 있습니까?

도움이 되었습니까?

해결책

기본적으로 생성 된 코드에서 주입 지점을 정의하는 방법과 Python / Ruby / Whatev 스크립트를 실행하는 방법은 두 가지 문제가 있습니다.

프로세스를 생성하는 방법에 따라 가능한 한 가지 솔루션은 가능한 각 주입 지점에 함수를 추가하는 것입니다. 기능은 사용자가 주어진 포인트와 스크립트를 연결했는지 여부를 확인하고 그렇다면 호출하여 스크립트를 실행합니다. Ironpython / 아이언 루비 (선택적으로 주어진 매개 변수).

단점은 다음과 같습니다. 스크립트에서 생성 된 프로세스로의 접근성이 제한되어 있습니다 (기본적으로 매개 변수로 전달 된 변수 만 액세스 할 수 있음); 구현 한도뿐만 아니라 (Ironpython의 현재 버전은 여러 기본 시스템 기능을 생략합니다).

다른 팁

Ironpython과 Ironruby를 살펴보십시오.이를 통해 C#과 쉽게 상호 작용할 수 있습니다.

C# 응용 프로그램 내에서 C# 코드를 컴파일 할 수 있습니다. csharpcodeprovider 수업.

컴파일이 성공하면 결과 어셈블리를 compiledassembly 의 속성 compilerresults 수업.

Awesome C# scripting language - Script.Net

.NET has a scripting language including runtime engine in PowerShell which can be embedded in any .NET application.

You can compile C# code "on the fly" into an in-memory assembly. I think this is possible with IronPython and IronRuby as well. Look at the CodeDomProvider.CreateProvider method.

If you need to run scripts a lot, or if your process runs for a long time, you might want to load these assemblies into another AppDomain. And unload the AppDomain after you're done with the script. Otherwise you are unable to remove them from memory. This has some consequenses on the other classes in your project, because you have to marshall all calls.

Have you thought about Visual Studio for Applications? I haven't heard much about it since .NET 1.1, but it might be worth a look.

http://msdn.microsoft.com/en-us/library/ms974548.aspx

I've done exactly this just recently - allowed run-time addition of C# scripting.

It's not hard at all, and this article:

http://www.divil.co.uk/net/articles/plugins/scripting.asp

is a very useful summary of the details.

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