Instantiating custom C# classes from IronPython

随声附和 提交于 2019-11-29 15:11:52

问题


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!


回答1:


Assuming MyClass is in MyAssembly.dll:

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


来源:https://stackoverflow.com/questions/10870943/instantiating-custom-c-sharp-classes-from-ironpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!