IronPython脚本调用C#dll示例
2007-06-07 07:07
357 查看
上两篇IronPython脚本的文章介绍了与C#紧密结合的示例,这里还将提供一个与C#结合更紧密的示例,直接调用C#编写的DLL。
我们还是沿用了上篇文章的代码(其实这里可以直接使用IronPython调试器进行联调了,没有必要再嵌入到C#了)
注意:scriptEngine.AddToPath(Application.StartupPath); 这句代码比较关键,设定dll文件所在的目录。
开始编写可供IronPython脚本调用的DLL,我们编写了两个类,一个提供静态函数访问,另一个提供属性和普通函数访问,以区别在IronPython脚本不同调用的方式。代码如下:
下面再让我们看看IronPython脚本中的代码吧:
比较关键的是这两句:
静态方法可以直接调用,普通方法需要先定义类,再访问(和访问IronPython
自己本身的类没有任何区别)。
运行结果如下:
现在你是否对IronPython充满期待和兴趣了吧,动起手来,感受它的强大!
我们还是沿用了上篇文章的代码(其实这里可以直接使用IronPython调试器进行联调了,没有必要再嵌入到C#了)
注意:scriptEngine.AddToPath(Application.StartupPath); 这句代码比较关键,设定dll文件所在的目录。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using IronPython.Hosting; namespace TestIronPython { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { PythonEngine scriptEngine = new PythonEngine(); scriptEngine.AddToPath(Application.StartupPath); scriptEngine.Execute(textBox1.Text); } } }
开始编写可供IronPython脚本调用的DLL,我们编写了两个类,一个提供静态函数访问,另一个提供属性和普通函数访问,以区别在IronPython脚本不同调用的方式。代码如下:
using System; using System.Collections.Generic; using System.Text; namespace IronPython_TestDll { public class TestDll { public static int Add(int x, int y) { return x + y; } } public class TestDll1 { private int aaa = 11; public int AAA { get { return aaa; } set { aaa = value; } } public void ShowAAA() { global::System.Windows.Forms.MessageBox.Show(aaa.ToString()); } } }
下面再让我们看看IronPython脚本中的代码吧:
import clr clr.AddReferenceByPartialName("System.Windows.Forms") clr.AddReferenceByPartialName("System.Drawing") from System.Windows.Forms import * from System.Drawing import * clr.AddReferenceToFile("IronPython_TestDll.dll") from IronPython_TestDll import * a=12 b=6 c=TestDll.Add(a,b) MessageBox.Show(c.ToString()) td=TestDll1() td.AAA=100 td.ShowAAA()
比较关键的是这两句:
clr.AddReferenceToFile("TronPython_TestDll.dll") -- 加载DLL文件 from TronPython_TestDll import * -- 导入命名空间
静态方法可以直接调用,普通方法需要先定义类,再访问(和访问IronPython
自己本身的类没有任何区别)。
运行结果如下:
现在你是否对IronPython充满期待和兴趣了吧,动起手来,感受它的强大!
相关文章推荐
- IronPython脚本调用C#dll示例
- IronPython脚本调用C#dll示例
- IronPython脚本调用C#dll示例
- C#调用Python脚本的简单示例,IronPython(转载)
- 在C#环境中动态调用IronPython脚本
- QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)
- Python脚本调用C#代码数据交互示例(hello world)
- C#调用Python脚本的简单示例
- 在C#环境中动态调用IronPython脚本 .
- C#与IronPython脚本进行数据交互示例
- C# 调用 Delphi Dll链接库方法及示例
- C#与IronPython脚本进行数据交互示例
- QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)
- QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)good
- C#调用C语言生成的DLL示例
- 在C#环境中动态调用IronPython脚本(一)
- C#调用winmm.dll来放音乐的示例[转载]
- 在C#环境中动态调用IronPython脚本(一)
- C#调用C(C++)dll示例教程,包含dll找不到函数入口的解决办法,包含C#dll调用方式不对出错的解决办法
- QT编写DLL给外部程序调用,提供VC/C#/C调用示例(含事件)