Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating custom C# classes from IronPython

Is there any way to make a class available to IronPython scripts so that I can create objects inside the code?

For example, if I have a class that I want to be able to instantiate from the script called MyClass defined in the C# code like so:

public class MyClass
{
    string text;

    public MyClass(string text)
    {
        this.text = text;
    }

    public void Write()
    {
        Console.WriteLine(text);
    }
}

How can I do this in the Python script?

obj = MyClass("Hello, World!")
obj.Write()

Thanks!

like image 553
jmegaffin Avatar asked Jun 03 '12 13:06

jmegaffin


1 Answers

Assuming MyClass is in MyAssembly.dll:

import clr
clr.AddReference('MyAssembly.dll')
import MyClass
obj = MyClass("Hello, World!")
obj.Write()
like image 118
Tim S. Avatar answered Oct 03 '22 22:10

Tim S.