Accessing C# class members in IronPython

妖精的绣舞 提交于 2019-12-22 04:16:10

问题


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

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