Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing C# class members in IronPython

Tags:

c#

ironpython

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?

like image 890
peacemaker Avatar asked Jun 14 '12 20:06

peacemaker


People also ask

What is C$ admin share?

The c$ share is an administrative share that the cluster or SVM administrator can use to access and manage the SVM root volume. The following are characteristics of the c$ share: The path for this share is always the path to the SVM root volume and cannot be modified.


1 Answers

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.

like image 58
Jon Skeet Avatar answered Oct 13 '22 07:10

Jon Skeet