Creating Custom C # Classes from IronPython

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

For example, if I have a class that I want to get from a script called MyClass defined in C # code, for example:

public class MyClass
{
    string text;

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

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

How can I do this in a Python script?

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

Thank!

+5
source share
1 answer

Assuming MyClass is located in MyAssembly.dll:

import clr
clr.AddReference('MyAssembly.dll')
import MyClass
obj = MyClass("Hello, World!")
obj.Write()
+7
source

All Articles