从python调用C#dll文件

问题描述

我的 C# 文件 计算器.c

    using System;
namespace DynamicCS
   {
      public class Calculator 
      {
         public double add(double argA,double argB)
         {
             return argA + argB;
         }
         public double sub(double argA,double argB)
         {
             return argA - argB;
         }
      }
   }

DynamicCalc.cs 文件

using System;
using System.Dynamic;

namespace DynamicCS
{
    public class DynamicCalc : DynamicObject
    {
        Calculator calc;
        public DynamicCalc()
        {
            calc = new Calculator();
        }
        public override bool TryGetMember(GetMemberBinder binder,out object result)
        {
            result = null;
            switch (binder.Name)
            {
                case "add":
                    result = (Func<double,double,double>)((double a,double b)
                          => calc.add(a,b));
                    return true;
                case "sub":
                    result = (Func<double,double b)
                          => calc.sub(a,b));
                    return true;
            }
            return false;
        }
    }
}

我的python文件

import sys
sys.path.append(r"C:\Users\djdev\OneDrive\Desktop\hell\bin\Debug\netstandard2.0")
 
import clr
clr.AddReference(r"C:\Users\djdev\OneDrive\Desktop\hell\bin\Debug\netstandard2.0\DynamicCS.dll")
 
from DynamicCS import DynamicCalc
 
calc=DynamicCalc()
 
print (calc.__class__.__name__)
# display the name of the class: 'DynamicCalc' 
 
a=7.5
b=2.5
 
res = calc.add(a,b)
print (a,'+',b,'=',res)
 
res = calc.sub(a,'-',res)
 
raw_input('Press any key to finish...')

我的错误

C:\Users\djdev\OneDrive\Desktop\hell>python client.py
Traceback (most recent call last):
  File "C:\Users\djdev\OneDrive\Desktop\hell\client.py",line 5,in <module>
    clr.AddReference(r"C:\Users\djdev\OneDrive\Desktop\hell\bin\Debug\netstandard2.0\DynamicCS.dll")
System.IO.FileLoadException: Could not load file or assembly 'C:\\Users\\djdev\\OneDrive\\Desktop\\hell\\bin\\Debug\\netstandard2.0\\DynamicCS.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
File name: 'C:\\Users\\djdev\\OneDrive\\Desktop\\hell\\bin\\Debug\\netstandard2.0\\DynamicCS.dll'
   at System.Reflection.AssemblyName.nInit(RuntimeAssembly& assembly,Boolean forIntrospection,Boolean raiseResolveEvent)
   at System.Reflection.RuntimeAssembly.CreateAssemblyName(String assemblyString,RuntimeAssembly& assemblyFromresolveEvent)
   at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,Evidence assemblySecurity,StackCrawlMark& stackMark,IntPtr pPrivHostBinder,Boolean forIntrospection)
   at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,Boolean forIntrospection)
   at System.Reflection.Assembly.Load(String assemblyString)
   at Python.Runtime.AssemblyManager.LoadAssembly(String name)
   at Python.Runtime.CLRModule.AddReference(String name)

Dll 文件无法加载文件或程序集......这对我当前的项目非常重要 文件路径也是正确的.... 我什至尝试过 ctypes 模块,但错误是一样的,请告诉我我该怎么办..

#######################谢谢#################### ########

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)