Jump to content
  • Advertisement
Sign in to follow this  

Can IronPython access members of a C# class instance?

This topic is 2401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I can get an instance of a C# class just fine to IronPython, but I can't seem to do much of anything with it's members or functions. Is there anyway to get around this problem? The IronPython documentation and my searches hasn't led me very far on figuring this out for the past several hours.

Here is a simplified example of what I'm trying to do:
class Unit
public int HP { get; set; }
public int Attack { get; set; }

public Unit(int HP, int Attack)
this.HP = HP;
this.Attack = Attack;
public void PointlessFunction() { }
public override string ToString()
return "HP: " + HP.ToString() + " Attack: " + Attack.ToString();

class Program
static void Main(string[] args)
ScriptEngine Engine = Python.CreateEngine();
ScriptScope Scope = Engine.CreateScope();

Scope.SetVariable("Unit", DynamicHelpers.GetPythonTypeFromType(typeof(Unit)));

// declare instance of Unit
Engine.Execute("u = Unit(10,5)", Scope);

// display Unit instance
Engine.Execute("print u", Scope);
//HP: 10 Attack: 5

// Try to change something
Engine.Execute("u.HP = 20", Scope);
//Unhandled Exception: System.MissingMemberException: 'Unit' object has no attribute 'HP'

// Try to run a function
Engine.Execute("u.PointlessFunction()", Scope);
//Unhandled Exception: System.MissingMemberException: 'Unit' object has no attribute 'PointlessFunction'

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!