2

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.

In C# I would have something like:

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:

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 --

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

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

Or am I missing something altogether?

Regards, Chad

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

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;

}
3
  • It looks like you're explicitly asking for the base method to be called - does your actual code do something else? Commented May 22, 2012 at 15:27
  • Yes, actual code does "work" -- I just setup the tests so I could set breakpoints in the code... none of the breakpoints in the overrides get "hit" Commented May 22, 2012 at 17:51
  • 1
    Try print statements; it may be an issue with the debugger. I'm confused because it should work. Commented May 22, 2012 at 18:48

1 Answer 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.

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;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Good point -- fixed my made up code... unfortunately it doesn't fix the problem.
I ran your code and as soon as I declared the methods in the base class the runtime bound to the python implementation... How are you creating your objects/instances? How are you verifying what happens?
Added code for creating class in original question... I'm verifying what happens by breakpoints getting hit in Visual Studio -- main and init get hit, the others do not. -- thanks!
Could you add print statements to def get_SomeProperty(self), def set_SomeProperty(self, value) and def SomeMethod(self, atTime) as Jeff suggested? This is what I did and as soon as I marked the base implementation virtual the python implementation got called.
Apologies! I was testing overrides on properties that were not set to virtual... A 2 week vacation cleared the head and allowed me to actually read your comment and check my code. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.