从 C# 调用 python 脚本并将参数动态传递给函数

Invoke python script from C# and pass parameters to a function dynamically

如何从 C# 动态调用 python 代码并动态传递参数。

Python脚本可以接受2个或3个0r 4..参数。

当我调用函数时出现错误提示“str is not callable” 这是我的 python 脚本

#this method can accept 2 or 3 or 4 or--parameters dynamically
 class AddTest:   
  def CalcAdd(self, Numb1, Numb2):
    return Numb1 + Numb2;

这是我的 C# 代码

  var engine = Python.CreateEngine();
        var scope = engine.CreateScope();
  
        ObjectOperations ops = engine.CreateOperations();

        var compilerOptions = (PythonCompilerOptions)engine.GetCompilerOptions();
        //compilerOptions.Module = IronPython.Runtime.ModuleOptions.co
        //ErrorSink errorSink = null;
        //ErrorListener errorListener = new ErrorSinkProxyListener(errorSink);
        var scriptSource = engine.CreateScriptSourceFromFile(@"C:\Nidec\PythonScript\download_nrlist.py", Encoding.UTF8, Microsoft.Scripting.SourceCodeKind.File);
        try
        {
            var compiledCode = scriptSource.Compile();
            compiledCode.Execute(scope);
            var pyScope = engine.ExecuteFile(@"C:\Nidec\PythonScript\download_nrlist.py", scope);            
            var variables = pyScope.GetVariableNames();
            var type = variables.ToList().Where(x => !x.Contains("_")).ToList().FirstOrDefault();
            var clsName = scope.GetVariable(type);     
            var clsObje = ops.CreateInstance(clsName);             
            var f = ops.GetMember(clsObje, "CalcAdd");
            var __func__ = ops.GetMember(f, "__func__");
            var t = __func__.GetType();
            PropertyInfo property = t.GetProperty("ArgNames", BindingFlags.Instance | BindingFlags.NonPublic);
            var arguments = property.GetValue(__func__, null) as string[];
            object[] obj = new object[arguments.Length-1] ;
           //int[] obj = new int[arguments.Length-1] ;
            for (int i = 0; i < arguments.Length-1; i++)
            {
                obj[i] = i + 1;
            }
            var ccResult = ops.Invoke("CalcAdd", obj);// error str is not callable
        }
object pythonClass = engine.Operations.Invoke(scope.GetVariable(clsName)); 

 var results = engine.Operations.InvokeMember(pythonClass, "CalcAdd", obj);