问题
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