本文主要是介绍IronPython脚本调用C#实现的dll库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C#代码 public partial class Form1 : Form
{
public static PythonEngine engine;
private static ClrModule clr;
private void InitializePythonEngine()
{
engine = new PythonEngine();
clr = (ClrModule)engine.Import("clr");
engine.Globals.Add("SysPath",
System.Environment.GetFolderPath(System.Environment.SpecialFolder.System)+"/");
}
public Form1()
{
InitializeComponent();
InitializePythonEngine();
}
private void button1_Click(object sender, EventArgs e)
{
scriptEngine.Execute(textBox1.Text);
}
}
{
public static PythonEngine engine;
private static ClrModule clr;
private void InitializePythonEngine()
{
engine = new PythonEngine();
clr = (ClrModule)engine.Import("clr");
engine.Globals.Add("SysPath",
System.Environment.GetFolderPath(System.Environment.SpecialFolder.System)+"/");
}
public Form1()
{
InitializeComponent();
InitializePythonEngine();
}
private void button1_Click(object sender, EventArgs e)
{
scriptEngine.Execute(textBox1.Text);
}
}
编译C#文件为类库
csc /t:library /out:1TestDll.dll *.cs
IronPython中调用动态库中自定义的类
import clr
import System
clr.AddReference( " System.Windows.Forms " )
clr.AddReference( " System.Drawing " )
from System.Windows.Forms import *
from System.Drawing import *
from System.IO import *
from System.Reflection import *
# 用python也可以实现相同的功能
# b = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) +"/"
# dllfilename = b + "TestDll.dll"
# 调用动态库
dllfilename = SysPath + " TestDll.dll "
clr.AddReferenceToFileAndPath(dllfilename)
from TestDll import *
f = Form1()
f.ShowDialog()
import System
clr.AddReference( " System.Windows.Forms " )
clr.AddReference( " System.Drawing " )
from System.Windows.Forms import *
from System.Drawing import *
from System.IO import *
from System.Reflection import *
# 用python也可以实现相同的功能
# b = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) +"/"
# dllfilename = b + "TestDll.dll"
# 调用动态库
dllfilename = SysPath + " TestDll.dll "
clr.AddReferenceToFileAndPath(dllfilename)
from TestDll import *
f = Form1()
f.ShowDialog()
附录A
csc编译动态库参数说明:
csc /t:library /out:文件名.dll /r:引用文件名(包含路径) /recurse:包含的文件(含路径) *.cs
这篇关于IronPython脚本调用C#实现的dll库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!