call C# dll file from python

0

my C# file Calculator.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 file

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;
        }
    }
}

my python file

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...')

my error is

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 file Could not load file or assembly...... This is very important for my current project file path is also correct.... I even tried ctypes module but error is same please tell me what should I do..

########################Thank You#############################

python
c#
ctypes
python.net

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0