4

Right now I can use following code to access my c# types in IronPython as follow

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

however I don't want script developers to have clr.AddReference('myDLL.dll') line in Python source code, and inject myDLL.dll (and/or a c# class) directly from c# into ScriptEngine so the previous code will be something similar to:

import myType

obj = myType()  

how can I achieve this?

1
  • Perhaps distribute a python module with your dll that does the AddReference and imports myType. So users of your lib would only import the extra python module. Commented Jun 17, 2017 at 10:57

1 Answer 1

6

You can solve this problem using 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 python script you can write:

from MyNameSpace import *
n=MyClass()
print n.DoSomeThing()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.