IronPython在C#中将命名空间添加到范围

时间:2023-01-17 14:21:13

I have following C# code

我有以下C#代码

namespace API
{
    public class AutoRcu
    {
        private ...

        public AutoRcu() 
        {
            ...
        }

        public void pressKey(string name)
        {
            ...
        }
    ...
}

I am running following IronPython code to operate C# code.

我正在运行IronPython代码来运行C#代码。

rcu.pressKey("Menu")

Ths works fine but the question is:
I would like to change Python API to run:

这样做很好,但问题是:我想改变Python API来运行:

API.rcu.pressKey()

instead of

代替

rcu.pressKey()

How to do that?

怎么做?

Now I add such a class by using

现在我通过使用添加这样一个类

pyScope.SetVariable("rcu",AutoRcu)   

function.

功能。

1 个解决方案

#1


1  

Well, you're essentially creating an object that has a property rcu that is an instance of your AutoRcu class. Just create the object.

好吧,你实际上是在创建一个具有属性rcu的对象,它是AutoRcu类的一个实例。只需创建对象。

dynamic api = new ExpandoObject();
api.rcu = new AutoRcu();
pyScope.SetVariable("API", api);

#1


1  

Well, you're essentially creating an object that has a property rcu that is an instance of your AutoRcu class. Just create the object.

好吧,你实际上是在创建一个具有属性rcu的对象,它是AutoRcu类的一个实例。只需创建对象。

dynamic api = new ExpandoObject();
api.rcu = new AutoRcu();
pyScope.SetVariable("API", api);