Python 调用.net framework写的类库的方法

版权声明

请尊重原创作品。转载请保持文章完整性,并以超链接形式注明原始作者“tingsking18”和主站点地址,方便其他朋友提问和指正。

Python 调用.net framework写的类库的方法

1. 安装python2.5和对应的pywin32

安装方法:略。

2. 下载ironPython源代码 C#的。

我用的是IronPython1.1的版本,从codeplex网站下载IronPython_1_1.rar。解压缩。

3. vistual studio 建立一个.NET类库工程,工程名字为:IronPython

Python 调用.net framework写的类库的方法_第1张图片

ironPython目录下的src子目录中的ironPython目录的ironMath目录里的c#源文件(不要assemblyInfo.cs)都考到你的工程目录里

Python 调用.net framework写的类库的方法_第2张图片

在工程里增加刚考来的源文件。

Python 调用.net framework写的类库的方法_第3张图片

4. 修改AssemblyInfo.cs

[assembly: ComVisible(false)]

false 改为true

5. 在项目=>属性=>生成里把 COM Interop注册 打上勾

Python 调用.net framework写的类库的方法_第4张图片

6. 在项目=>属性=>签名 里把为程序集签名 打上勾,选择强名称密钥文件时 选择新建,输个文件名,比如 IronPython.snk, 也可以手动生成密钥文件,然后在这里选择.

手动生成的方法:.Net Framework的命令行界面里用 sn -k IronPython.snk 这个命令.

Python 调用.net framework写的类库的方法_第5张图片

7. 编译项目生成 IronPython.dll

8. .Net Framework的命令行界面里用 regasm IronPython.dll 注册COM组件

9. .Net Framework的命令行界面里用 gacutil -i IronPython.dll 把库加到全局缓存里

10. 建立.net framework 的类库。代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace test

{

public class testclass

{

public string func()

{

return "hello world";

}

}

}

11. 建立test.py调用刚才写的test.dll类库。代码:

import win32com

import win32com.client

eng=win32com.client.Dispatch("IronPython.Hosting.PythonEngine")

eng.execute("import clr")

eng.execute("import sys")

eng.execute("""sys.path.append('c:\\\\test')""")

eng.execute("print sys.path")

eng.execute("""clr.AddReferenceToFile("abc.dll")""")

eng.execute("from test import *")

eng.execute("obj = testclass()")

eng.execute("print obj.func()")

注意:eng.execute("""sys.path.append('c:\\\\test')""")

这句是将test.dll所在的路径添加到ironPythonsys.path中去。如果不加,AddReferenceToFile将会出错。

<!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} > <! [endif] > </div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">Python </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">调用</span><span lang="EN-US">.net framework</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">写的类库的方法</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">1. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">安装</span><span lang="EN-US">python2.5</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">和对应的</span><span lang="EN-US">pywin32</span></div> <div class="MsoNormal" style="text-indent: 20.25pt;" mce_style="text-indent: 20.25pt;" mce_tmp="1"><span style="font-family: 宋体;" mce_style="font-family: 宋体;">安装方法:略。</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">2. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">下载</span><span lang="EN-US">ironPython</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">源代码</span> <span style="font-family: 宋体;" mce_style="font-family: 宋体;">是</span><span lang="EN-US">C#</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">的。</span></div> <div class="MsoNormal" mce_tmp="1"><span style="font-family: 宋体;" mce_style="font-family: 宋体;">我用的是</span><span lang="EN-US">IronPython1.1</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">的版本,从</span><span lang="EN-US">codeplex</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">网站下载</span><a href="http://ironpython.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=2573#DownloadId=10616" mce_href="http://ironpython.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=2573#DownloadId=10616" target="_blank"><span lang="EN-US">IronPython_1_1.rar</span></a><span style="font-family: 宋体;" mce_style="font-family: 宋体;">。解压缩。</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">3. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">用</span><span lang="EN-US">vistual studio </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">建立一个</span><span lang="EN-US">.NET</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">类库工程,工程名字为:</span><span lang="EN-US">IronPython</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">。把</span><span lang="EN-US">ironPython</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">目录下的</span><span lang="EN-US">src</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">子目录中的</span><span lang="EN-US">ironPython</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">目录的</span><span lang="EN-US">ironMath</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">目录里的</span><span lang="EN-US">c#</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">源文件</span><span lang="EN-US">(</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">不要</span><span lang="EN-US">assemblyInfo.cs)</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">都考到你的工程目录里。在工程里增加刚考来的源文件。</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">4. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">修改</span><span lang="EN-US">AssemblyInfo.cs</span></div> <div class="MsoNormal" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US">[<span style="color: blue;" mce_style="color: blue;">assembly</span>: <span style="color: #2b91af;" mce_style="color: #2b91af;">ComVisible</span>(<span style="color: blue;" mce_style="color: blue;">false</span>)]</span></div> <div class="MsoNormal" mce_tmp="1"><span style="font-family: 宋体;" mce_style="font-family: 宋体;">将</span><span lang="EN-US">false </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">改为</span><span lang="EN-US">true</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">5. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">在项目</span><span lang="EN-US">=&gt;</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">属性</span><span lang="EN-US">=&gt;</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">生成里把</span> <span style="font-family: 宋体;" mce_style="font-family: 宋体;">为</span><span lang="EN-US">COM Interop</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">注册</span> <span style="font-family: 宋体;" mce_style="font-family: 宋体;">打上勾</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">6. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">在项目</span><span lang="EN-US">=&gt;</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">属性</span><span lang="EN-US">=&gt;</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">签名</span> <span style="font-family: 宋体;" mce_style="font-family: 宋体;">里把为程序集签名</span> <span style="font-family: 宋体;" mce_style="font-family: 宋体;">打上勾</span><span lang="EN-US">,</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">选择强名称密钥文件时</span> <span style="font-family: 宋体;" mce_style="font-family: 宋体;">选择新建</span><span lang="EN-US">,</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">输个文件名</span><span lang="EN-US">,</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">比如</span><span lang="EN-US"> IronPython.snk, </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">也可以手动生成密钥文件</span><span lang="EN-US">,</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">然后在这里选择</span><span lang="EN-US">.</span></div> <div class="MsoNormal" mce_tmp="1"><span style="font-family: 宋体;" mce_style="font-family: 宋体;">手动生成的方法</span><span lang="EN-US">:</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">在</span><span lang="EN-US">.Net Framework</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">的命令行界面里用</span><span lang="EN-US"> sn -k IronPython.snk </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">这个命令</span><span lang="EN-US">.</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">7. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">编译项目生成</span><span lang="EN-US"> IronPython.dll</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">8. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">在</span><span lang="EN-US">.Net Framework</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">的命令行界面里用</span><span lang="EN-US"> regasm IronPython.dll </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">注册</span><span lang="EN-US">COM</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">组件</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">9. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">在</span><span lang="EN-US">.Net Framework</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">的命令行界面里用</span><span lang="EN-US"> gacutil -i IronPython.dll </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">把库加到全局缓存里</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">10. </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">建立</span><span lang="EN-US">.net framework </span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">的类库。代码如下:</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体; color: blue;" mce_style="font-size: 10pt; font-family: 新宋体; color: blue;" lang="EN-US">using</span><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"> System;</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体; color: blue;" mce_style="font-size: 10pt; font-family: 新宋体; color: blue;" lang="EN-US">using</span><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"> System.Collections.Generic;</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体; color: blue;" mce_style="font-size: 10pt; font-family: 新宋体; color: blue;" lang="EN-US">using</span><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"> System.Linq;</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体; color: blue;" mce_style="font-size: 10pt; font-family: 新宋体; color: blue;" lang="EN-US">using</span><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"> System.Text;</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US">&nbsp;</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体; color: blue;" mce_style="font-size: 10pt; font-family: 新宋体; color: blue;" lang="EN-US">namespace</span><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"> test</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US">{</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span><span style="color: blue;" mce_style="color: blue;">public</span> <span style="color: blue;" mce_style="color: blue;">class</span> <span style="color: #2b91af;" mce_style="color: #2b91af;">testclass</span></span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span>{</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;" mce_style="color: blue;">public</span> <span style="color: blue;" mce_style="color: blue;">string</span> func()</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: blue;" mce_style="color: blue;">return</span> <span style="color: #a31515;" mce_style="color: #a31515;">"hello world"</span>;</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</span></div> <div class="MsoNormal" style="text-align: left;" mce_style="text-align: left;" align="left" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US"><span>&nbsp;&nbsp;&nbsp; </span>}</span></div> <div class="MsoNormal" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US">}</span></div> <div class="MsoNormal" mce_tmp="1"><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;" lang="EN-US">11. </span><span style="font-size: 10pt; font-family: 新宋体;" mce_style="font-size: 10pt; font-family: 新宋体;">建立<span lang="EN-US">test.py</span>调用刚才写的<span lang="EN-US">test.dll</span>类库。代码:</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">import win32com</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">import win32com.client</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">eng=win32com.client.Dispatch("IronPython.Hosting.PythonEngine")</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">&nbsp;</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">eng.execute("import clr")</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">eng.execute("import sys")</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">eng.execute("""sys.path.append('c:\\\\test')""")</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">eng.execute("print sys.path")</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">eng.execute("""clr.AddReferenceToFile("abc.dll")""")</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">eng.execute("from test import *")</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">eng.execute("obj = testclass()")</span></div> <div class="MsoNormal" mce_tmp="1"><span lang="EN-US">eng.execute("print obj.func()")</span></div> <div class="MsoNormal" mce_tmp="1"><span style="font-family: 宋体;" mce_style="font-family: 宋体;">注意:</span><span lang="EN-US">eng.execute("""sys.path.append('c:\\\\test')""")</span></div> <div class="MsoNormal" mce_tmp="1"><span style="font-family: 宋体;" mce_style="font-family: 宋体;">这句是将</span><span lang="EN-US">test.dll</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">所在的路径添加到</span><span lang="EN-US">ironPython</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">的</span><span lang="EN-US">sys.path</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">中去。如果不加,</span><span lang="EN-US">AddReferenceToFile</span><span style="font-family: 宋体;" mce_style="font-family: 宋体;">将会出错。在运行ipy的时候ipy所在的路径会直接添加到sys.path中去。编译成dll之后就不会自动添加进去了,所以要自己手动把test.dll的路径添加到sys.path中去。</span></div> <div mce_tmp="1">&nbsp;< ></div> < >< >< ></d-->

你可能感兴趣的:(.net,python,LINQ)