C# IronPython Excel Dna加载项-有关Microsoft.Dynamic引用的异常
我正在开始使用IronPython和一些C#作为调用IronPython的包装,开发一个Excel DNA加载项。在ExcelDNA开发人员的慷慨帮助下,我已经完成了一些初始的准备和运行示例的工作,但是现在我正在尝试在SharpDevelop中调试加载项,并且遇到了一些问题。由于我对其中大部分内容都是新手,所以我不确定这是否是SharpDevelop、.NET、Excel DNA或IronPython的问题 我在一个解决方案中创建了两个项目,一个是C#类库。另一个是python类库。我在一个博客上找到了一个错误,然后将项目设置为调试。我能够一步一步地完成C#代码的前几行,因此这就是进度,但当我到达下一行时:C# IronPython Excel Dna加载项-有关Microsoft.Dynamic引用的异常,c#,.net,ironpython,sharpdevelop,excel-dna,C#,.net,Ironpython,Sharpdevelop,Excel Dna,我正在开始使用IronPython和一些C#作为调用IronPython的包装,开发一个Excel DNA加载项。在ExcelDNA开发人员的慷慨帮助下,我已经完成了一些初始的准备和运行示例的工作,但是现在我正在尝试在SharpDevelop中调试加载项,并且遇到了一些问题。由于我对其中大部分内容都是新手,所以我不确定这是否是SharpDevelop、.NET、Excel DNA或IronPython的问题 我在一个解决方案中创建了两个项目,一个是C#类库。另一个是python类库。我在一个博客
pyEngine.Runtime.LoadAssembly(myclass);
我得到一个例外:
“无法加载文件或程序集
'Microsoft.Dynamic,版本=1.0.0.0,
文化=中立,
PublicKeyToken=31bf3856ad364e35'或
它的一个依赖项。位于
程序集的清单定义不正确
与程序集引用不匹配。
(HRESULT的例外:0x8013100)
但是我很确定我已经在我的项目中添加了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;
}
}
}
您的IronPython可能需要在.NET4运行时下运行。要让ExcelDNA加载.NET4,必须在主.DNA文件中显式添加RuntimeVersion属性。您的.dna文件将以以下内容开头:
<DnaLibrary RuntimeVersion="v4.0"> ... </DnaLibrary>
。。。
如果省略了该属性,则默认行为是加载运行时的.NET 2.0版本(也由.NET 3.0和3.5使用)
在.NET2.0运行时下托管IronPython是可能的,但是您需要自己处理DLR库,因为它们是内置的,并且已经与.NET4一起安装
<DnaLibrary RuntimeVersion="v4.0"> ... </DnaLibrary>