从IronPython的实例化定制的C#类 [英] Instantiating custom C# classes from IronPython

查看:214
本文介绍了从IronPython的实例化定制的C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法,使一类可用于IronPython的脚本,这样我可以创建代码中的对象?

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

例如,如果我有一个类,我希望能够从C#代码中定义的,像这样的脚本调用MyClass的实例:

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);
    }
}



我怎样才能在Python脚本做到这一点?

How can I do this in the Python script?

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

谢谢!

推荐答案

假设MyClass的是MyAssembly.dll程序:

Assuming MyClass is in MyAssembly.dll:

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

这篇关于从IronPython的实例化定制的C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆