Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Can IronPython access members of a C# class instance?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Daichi   Members   -  Reputation: 122

Like
0Likes
Like

Posted 20 March 2012 - 08:51 PM

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'
	}
}


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS