Pass a C# Class Object to a Python file

筅森魡賤 提交于 2019-12-07 12:03:17

问题


Okay... I'm trying to pass an Object which I declare in C# to a Python file with IronPython.

So this is what I was thinking off:

C# class:

public class ParamObj
{
   private string Obj_String;
   private int Obj_Int;

   public ParamObj(string lObjS, string lObjI)
   {
      Obj_String = lObjS;
      Obj_Int = lObjI;
   }

   public string getString()
   {
      return Obj_String;
   }

   public int getInt()
   {
      return Obj_Int;
   }

   public setString(string lStr)
   {
      Obj_String = lStr;
   }

   public setInt(int lInt)
   {
      Obj_Int = lInt;
   }
}

So i have my Class.. Now i declare the Class in my C# Project and i pass it to the PYthon file:

ParamObj Test_Object = new ParamObj("Test Object from C#", 1337);
var ipy = Python.CreateRuntime();
dynamic Test = ipy.UseFile("Test.py");

Test_Object = Test.ObjectAsParam(Test_Object);

Console.WriteLine(Test_Object.getString(), Convert.ToString(Test_Object.getInt()));

And now I want to use the Object in my Pythoin file and call it's methods. So and the Code in my Test.py is as follows:

def ObjectAsParam(Obj):

    print Obj.getString()
    print str(Obj.getInt())

    Obj.setString("Changed!")
    Obj.setInt(1338)

    return Obj

But my Compiler says that the python script doesn't know the Methods getString(), getInt() etc.

So is there a way to manage that ?

Thanks


回答1:


Make sure ParamObj is a public class.



来源:https://stackoverflow.com/questions/13340155/pass-a-c-sharp-class-object-to-a-python-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!