问题
In my C# code I have a class which stores some data I wish to pass down to my python code in a List. However, when I try to access properties of that class inside my python code I get MissingMemberException
. Here some example code to show what I mean:
C#:
class Event
{
public int EventId { get; set; }
public string EventName { get; set; }
}
//other processing here...
//this just fills the list with event objects
List<Event> eventList = GetEvents();
//this sets a variable in the ScriptScope
PythonEngine.SetVariable( "events", eventList);
PythonEngine.Execute("eventParser.py");
eventParser.py:
for e in events:
print e.EventId, " / ", e.EventName
The MissingMemberException
says "Event contains no member named EventId"
I have tested passing other types to the python, including lists of primitive types like List< int >
and List< string >
and they work fine.
So how do I access these class properties, EventId
and EventName
in my python script?
回答1:
Try making the Event
class public. The problem may be that although the property is public, the type is internal
by default, and so the dynamic typing doesn't "see" any of the members which are only declared by that type.
It's just a guess, and if it's wrong, please say so I can delete the answer and avoid confusing anyone in the future. You do get the same effect from using anonymous types in one assembly via dynamic typing in another assembly just within C# though.
来源:https://stackoverflow.com/questions/11041244/accessing-c-sharp-class-members-in-ironpython