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#############################

4
  • What version of python are you using? Check if this helps: stackoverflow.com/questions/50564191/… Commented Mar 2, 2021 at 8:09
  • try to use pythonnet Commented Mar 2, 2021 at 8:21
  • I am using python 3.9 @Quercus Commented Mar 2, 2021 at 9:02
  • I already using pythonnet @rdr20 Commented Mar 2, 2021 at 9:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.