Accessing C# class members in IronPython
Asked Answered
S

1

8

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?

Senegal answered 14/6, 2012 at 20:58 Comment(0)
P
15

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.

Prevocalic answered 14/6, 2012 at 21:1 Comment(1)
Thanks Jon, that did the job.Senegal

© 2022 - 2024 — McMap. All rights reserved.