Access C # class members in IronPython

In my C # code, I have a class that stores some data that I want to pass to my python code in List. However, when I try to access the properties of this class inside my python code, I get a MissingMemberException . Here is a sample code to show what I mean:

FROM#:

 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 

MissingMemberException says: "The event does not contain a member named EventId"

I tested passing other types to 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?

+8
c # ironpython
source share
1 answer

Try making the Event class public. The problem may be that, although the property is publicly available, the type is internal by default, and therefore dynamic typing does not "see" any of the members that are only declared by this type.

This is just a hunch, and if it is wrong, say that I can delete the answer and not confuse anyone in the future. You get the same effect of using anonymous types in one assembly by using dynamic input in another assembly only inside C #.

+14
source share

All Articles