您的位置:首页 > 编程语言 > Python开发

IronPython脚本调用C#实现的dll库

2007-06-05 23:36 891 查看
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);


}


}

编译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()

附录A
csc编译动态库参数说明:
csc /t:library /out:文件名.dll /r:引用文件名(包含路径) /recurse:包含的文件(含路径) *.cs
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: