C # type implementation in Ironpython

Now I can use the following code to access my c#types in IronPythonas follows

import clr
clr.AddReference('myDLL.dll')
import myType
obj = myType()

however, I don't want the script developers to have a clr.AddReference('myDLL.dll')line in the Pythonsource code and embed myDLL.dll(and / or class c#) directly from c#in ScriptEngine, so the previous code will look like:

import myType

obj = myType()  

how can i achieve this

+4
source share
1 answer

You can solve this problem using the following solution:

ScriptRuntime runtime = Python.CreateRuntime();
runtime.LoadAssembly(Assembly.GetAssembly(typeof(MyNameSpace.MyClass)));
ScriptEngine eng = runtime.GetEngine("py");
ScriptScope scope = eng.CreateScope();
ScriptSource src = eng.CreateScriptSourceFromString(MySource, SourceCodeKind.Statements);
var result = src.Execute(scope);

Now, in a python script, you can write:

from MyNameSpace import *
n=MyClass()
print n.DoSomeThing()
+4
source

Source: https://habr.com/ru/post/1679509/


All Articles