如何覆盖IronPython中的C#属性和方法?

时间:2021-08-24 06:29:39

I have an interface with a base class in C# -- I'd like to be able to implement derived classes in IronPython for embedded extensibility.

我在C#中有一个带基类的接口 - 我希望能够在IronPython中实现派生类的嵌入式可扩展性。

In C# I would have something like:

在C#我会有类似的东西:

public interface IInterface
{
    bool SomeProperty { get; set; }
    bool SomeMethod(DateTime atTime);
}

public abstract class BaseClass : IInterface
{
    public BaseClass() {}

    private bool someProperty = true;
    public virtual bool SomeProperty
    {
        get { return someProperty; }
        set { someProperty = value; }
    }

    public virtual bool SomeMethod(DateTime atTime)
    {
        return true;
    }
}

Then a controller type class

然后是控制器类型类

public class SomeOtherClass
{
    List<IInterface> interfaceCollection = new List<IInterface>();

    ... factory here to create C# classes and IPy classes derived from BaseClass or IInterface ...

    interfaceCollection.Add(somePytonDerivedClass);
    foreach (var intExersize in interfaceCollection)
    {
        if (intExersize.SomeProperty == true)
        {
            intExersize.SomeMethod(DateTime.Now);
        }
    }
}

I'd like to do an impl in IronPython -- something like:

我想在IronPython中做一个impl - 类似于:

class BaseClassIPy (BaseClass):
def __new__(self):
    print("Made it into the class")
    return BaseClass.__new__(self)

def __init__(self):
    pass

def get_SomeProperty(self):
    return BaseClass.SomeProperty

def set_SomeProperty(self, value):
    BaseClass.SomeProperty = value

def SomeMethod(self, atTime):
    return BaseClass.SomeMethod(atTime)

The new and init methods being called correctly --

正确调用new和init方法 -

but when I call the properties and methods on the IPy class, the calls seem to go directly to the base classes...

但是当我调用IPy类的属性和方法时,调用似乎直接转到基类...

Is it a syntax issue? i.e. the IPy code is wrong?

这是语法问题吗?即IPy代码错了?

Or am I missing something altogether?

或者我完全错过了什么?

Regards, Chad

此致,乍得

---------------- Edit ----- method to inst python class:

----------------编辑-----方法到inst python类:

private IInterface GetScriptPlugInNode()
{

    IInterface node = null;

    string plugInScript = "c:\\BaseClassIPy.py";
    string plugInClass = "BaseClassIPy";

    var options = new Dictionary<string, object>();
    ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(options);
    setup.HostType = typeof(SelfContainedScriptHost);  //PAL impl
    setup.DebugMode = true;

    var pyRuntime = new ScriptRuntime(setup);
    var engineInstance = Python.GetEngine(pyRuntime);

    // Redirect search path to use embedded resources
    engineInstance.SetSearchPaths(new[] { String.Empty });

    var scope = engineInstance.CreateScope();

    ScriptSource source = engineInstance.CreateScriptSourceFromFile(plugInScript);

    source.Execute(scope);
    var typeClass = scope.GetVariable(plugInClass);
    var instance = engineInstance.Operations.CreateInstance(typeClass);
    node = instance;

    return node;

}

1 个解决方案

#1


1  

You have to change the interface property/method in your abstract base class to be virtual in order to allow the IronPython class to properly inherit from that.

您必须将抽象基类中的接口属性/方法更改为虚拟,以便允许IronPython类从中正确继承。

public abstract class BaseClass : IInterface
{
    public BaseClass() {}

    private bool someProperty = true;
    public virtual bool SomeProperty
    {
        get { return someProperty; }
        set { someProperty = value; }
    }

    public virtual bool SomeMethod(DateTime atTime)
    {
        return true;
    }
}

#1


1  

You have to change the interface property/method in your abstract base class to be virtual in order to allow the IronPython class to properly inherit from that.

您必须将抽象基类中的接口属性/方法更改为虚拟,以便允许IronPython类从中正确继承。

public abstract class BaseClass : IInterface
{
    public BaseClass() {}

    private bool someProperty = true;
    public virtual bool SomeProperty
    {
        get { return someProperty; }
        set { someProperty = value; }
    }

    public virtual bool SomeMethod(DateTime atTime)
    {
        return true;
    }
}