Andy

Located in Tucson, Arizona, USA

Homepage: http://www.britishideas.com


Posts by Andy

Accessing an Overloaded C# Constructor from IronPython

The C# class ‘Foo’ has four constructors. The glue between IronPython and C# is not choosing the correct one. Here is how to force it.

import clr
typ = clr.GetClrType(Foo)
ctor = typ.GetConstructors()[3]
newfoo = ctor.Invoke(System.Array[object](['string value', 1234]))

newfoo is now an instance of the Foo object, created using a string and int parameters. Here we are assuming the fourth constructor is the one we want, which isn’t very robust. Instead the constructors can be enumerated and the parameters evaluated to determine the correct constructor to use.