我在C#中有一个带基类的接口 - 我希望能够在IronPython中实现派生类的嵌入式可扩展性。
在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;
}
}
然后是控制器类型类
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);
}
}
}
我想在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)
正确调用新和 init 方法 -
但是当我调用IPy类的属性和方法时,调用似乎直接转到基类...
这是语法问题吗?即IPy代码错了?
或者我完全错过了什么?
问候,乍得
----------------编辑-----方法到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;
}
答案 0 :(得分:1)
您必须将抽象基类中的接口属性/方法更改为虚拟,以便允许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;
}
}