从python调用C#dll文件

2024-05-08 00:48:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我的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, double>)((double a, 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, b)
print (a, '-', b, '=', 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, Boolean forIntrospection, RuntimeAssembly& assemblyFromResolveEvent)
   at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
   at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, 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模块,但错误相同。请告诉我应该怎么做

摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩摩##############


Tags: 文件namestringcalconedrivepublicsystemusers