IronPython Excel-Dna加载项 - 关于Microsoft.Dynamic引用的例外

我开始使用IronPython开发Excel-DNA插件,并使用一些C#作为IronPython调用的包装器。在Excel-DNA开发人员的慷慨帮助下,我已经解决了启动和运行样本的一些初步问题,但现在我正在尝试调试SharpDevelop中的插件,我遇到了一些问题。由于我对大部分内容都是全新的,我不确定它是否是SharpDevelop,.NET,Excel-DNA或IronPython的问题。 我在一个解决方案中创建了两个项目,一个是C#类库。另一个是python类库。我按照我在博客上找到的教程设置项目进行调试。我能够逐步完成C#代码的前几行,所以这是进步,但是当我到达以下行时:
pyEngine.Runtime.LoadAssembly(myclass); 
我得到一个例外:   “无法加载文件或程序集   'Microsoft.Dynamic,版本= 1.0.0.0,   文化=中性,   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;
        }
    }
}
MyClass.py
class MyClass:
    def __init__(self):
        pass

    def AddSomeStuff(self,x,y):
        return x + y
    
已邀请:
你的IronPython东西可能需要在.NET 4运行时下运行。要告诉Excel-DNA加载.NET 4,您必须在主.dna文件中显式添加RuntimeVersion属性。您的.dna文件将以以下内容开头:
<DnaLibrary RuntimeVersion="v4.0"> ... </DnaLibrary>
如果省略该属性,则默认行为是加载.NET 2.0版本的运行时(.NET 3.0和3.5也使用该版本)。 可能可以在.NET 2.0运行时下托管IronPython,但是您需要自己处理DLR库,而它们是内置的并且已经安装了.NET 4。     

要回复问题请先登录注册