IronPython脚本调用C#dll示例

上两篇IronPython脚本的文章介绍了与C#紧密结合的示例,这里还将提供一个与C#结合更紧密的示例,直接调用C#编写的DLL。

      我们还是沿用了上篇文章的代码(其实这里可以直接使用IronPython调试器进行联调了,没有必要再嵌入到C#了)

    注意:scriptEngine.AddToPath(Application.StartupPath); 这句代码比较关键,设定dll文件所在的目录。

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using IronPython.Hosting;  
  9.  
  10. namespace TestIronPython  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.  
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             PythonEngine scriptEngine = new PythonEngine();  
  22.             scriptEngine.AddToPath(Application.StartupPath);   
  23.  
  24.             scriptEngine.Execute(textBox1.Text);            
  25.         }  
  26.     }  
  27. }  
  28.  

     开始编写可供IronPython脚本调用的DLL,我们编写了两个类,一个提供静态函数访问,另一个提供属性和普通函数访问,以区别在IronPython脚本不同调用的方式。代码如下:  

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace IronPython_TestDll  
  6. {  
  7.     public  class TestDll  
  8.     {  
  9.         public static int Add(int x, int y)  
  10.         {  
  11.             return x + y;  
  12.         }  
  13.     }  
  14.  
  15.     public class TestDll1  
  16.     {  
  17.         private int aaa = 11;  
  18.         public int AAA  
  19.         {  
  20.             get { return aaa; }  
  21.             set { aaa = value; }  
  22.         }  
  23.         public void ShowAAA()  
  24.         {  
  25.             global::System.Windows.Forms.MessageBox.Show(aaa.ToString());  
  26.         }  
  27.  
  28.     }  
  29. }  
  30.  

       下面再让我们看看IronPython脚本中的代码吧:

 
 
  1. import clr  
  2. clr.AddReferenceByPartialName("System.Windows.Forms")  
  3. clr.AddReferenceByPartialName("System.Drawing")  
  4. from System.Windows.Forms import *  
  5. from System.Drawing import *  
  6.  
  7. clr.AddReferenceToFile("IronPython_TestDll.dll")  
  8. from IronPython_TestDll import *  
  9.  
  10. a=12  
  11. b=6  
  12. c=TestDll.Add(a,b)  
  13. MessageBox.Show(c.ToString())  
  14.  
  15. td=TestDll1()  
  16. td.AAA=100  
  17. td.ShowAAA()  
  18.  

    比较关键的是这两句:

    

 
 
  1. clr.AddReferenceToFile("TronPython_TestDll.dll")    -- 加载DLL文件  
  2.    from TronPython_TestDll import *                                  -- 导入命名空间   

        静态方法可以直接调用,普通方法需要先定义类,再访问(和访问IronPython
自己本身的类没有任何区别)。

       运行结果如下:

     

        现在你是否对IronPython充满期待和兴趣了吧,动起手来,感受它的强大!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值