oob*_*oob 5 .net c# ironpython sharpdevelop excel-dna
我开始使用IronPython开发Excel-DNA插件,并使用一些C#作为IronPython调用的包装器.在Excel-DNA开发人员的慷慨帮助下,我已经解决了启动和运行样本的一些初步问题,但现在我正在尝试调试SharpDevelop中的插件,我遇到了一些问题.由于我对大部分内容都是全新的,我不确定它是否是SharpDevelop,.NET,Excel-DNA或IronPython的问题.
我在一个解决方案中创建了两个项目,一个是C#类库.另一个是python类库.我按照我在博客上找到的教程设置项目进行调试.我能够逐步完成C#代码的前几行,所以这是进步,但是当我到达以下行时:
pyEngine.Runtime.LoadAssembly(myclass);
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:
"无法加载文件或程序集'Microsoft.Dynamic,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.找到的程序集的清单定义与程序集引用不匹配.(HRESULT的异常:0x80131040 )"
但我很确定我已将Microsoft.Dynamic引用添加到我的项目中.它是版本1.1.0.20.这包含在IronPython发行版中,但也包含在我计算机上的其他位置.我已经尝试将引用设置为两者,但它们都具有相同的版本号并且看起来是相同的文件大小.两者都不起作用.我需要1.0.0.0版本还是我做错了什么?我真的不明白为什么pyEngine(Python.CreateEngine()返回的ScriptEngine)会尝试加载与发行版中包含的版本不同的版本.
代码如下.如果您需要任何其他信息,请与我们联系.
MyAddin.cs
/*
Added these references all as Local Copies - probably not necessary?
System.Windows.Forms
Microsoft.CSharp
ExcelDna.Integration (from Excel-DNA distribution folder)
IronPython (from IronPython folder)
IronPython.Modules (from IronPython folder)
Microsoft.Dynamic (from IronPython folder)
Microsoft.Scripting (from IronPython folder)
Microsoft.Scripting.Metadata (from IronPython folder)
mscorlib (I don't really know why I added this, but it was one of the references in my IronPython class library)
MyClass (this is the reference to my IronPython class - I checked to see that it gets copied in every time I rebuild the solution and it does)
These were automatically added by SharpDevelop when I created the project.
System
System.Core
System.Windows.Forms
System.Xml
System.Xml.Linq
*/
using System;
using System.IO;
using System.Windows.Forms;
using ExcelDna.Integration;
using System.Reflection;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
public class MyAddIn : IExcelAddIn
{
public void AutoOpen()
{
try
{
string xllDirectory = Path.GetDirectoryName(@"C:/Users/myname/Documents/SharpDevelop Projects/IronPythonExcelDNATest/MyAddIn/bin/Debug/");
string dllPath = Path.Combine(xllDirectory,"MyClass.dll");
Assembly myclass = Assembly.LoadFile(dllPath);
ScriptEngine pyEngine = Python.CreateEngine();
pyEngine.Runtime.LoadAssembly(myclass);
ScriptScope pyScope = pyEngine.Runtime.ImportModule("MyClass");
object myClass = pyEngine.Operations.Invoke(pyScope.GetVariable("MyClass"));
IronTest.AddSomeStuff = pyEngine.Operations.GetMember<Func<double, double,double>>(myClass, "AddSomeStuff");
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
public void AutoClose()
{
}
}
public class IronTest
{
public static Func<double, double, double> AddSomeStuff;
public static double TestIPAdd(double val1, double val2)
{
try
{
return AddSomeStuff(val1, val2);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return double.NaN;
}
}
}
Run Code Online (Sandbox Code Playgroud)
MyClass.py
class MyClass:
def __init__(self):
pass
def AddSomeStuff(self,x,y):
return x + y
Run Code Online (Sandbox Code Playgroud)
您的 IronPython 内容可能需要在 .NET 4 运行时下运行。要告诉 Excel-DNA 加载 .NET 4,您必须在主 .dna 文件中显式添加 RuntimeVersion 属性。您的 .dna 文件将以以下内容开头:
<DnaLibrary RuntimeVersion="v4.0"> ... </DnaLibrary>
Run Code Online (Sandbox Code Playgroud)
如果省略该属性,则默认行为是加载 .NET 2.0 版本的运行时(.NET 3.0 和 3.5 也使用该版本)。
可以在 .NET 2.0 运行时下托管 IronPython,但随后您需要自己处理 DLR 库,而它们是内置的并且已随 .NET 4 一起安装。
| 归档时间: |
|
| 查看次数: |
1840 次 |
| 最近记录: |