Explicitly implemented interface methods
Hello out there, I've come across an issue with calling a method of an instance which ends up being an explicitly implemented interface method. Let me explain with a very brief example: Consider this a very basic interface: [File: ISomeInterface.cs] namespace PythonInterfaceCalls { public interface ISomeInterface { void ImplicitImplementation(); void ExplicitImplementation(); } } And here's an implementing class. Note one of the methods being implicitly implemented, the other being explicitly implemented: [File: SomeImplementation.cs] using System; namespace PythonInterfaceCalls { public class SomeImplementation: ISomeInterface { public void ImplicitImplementation() { Console.WriteLine("Call of implicitly implemented interface method."); } void ISomeInterface.ExplicitImplementation() { Console.WriteLine("Call of explicitly implemented interface method."); } } } Note the method headers. While the implicit implementation is (and has to be) public, the explicit implementation is (and has to be) internal. Both methods work well in C# (as long as the instance is casted into the interface type). See the unit tests, both passing: [File: TestImplementation.cs] using System; using System.IO; using NUnit.Framework; namespace PythonInterfaceCalls { [TestFixture] public class TestImplementation { [Test] public void TestImplicitCall() { var implementation = new SomeImplementation() as ISomeInterface; using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); implementation.ImplicitImplementation(); Assert.AreEqual("Call of implicitly implemented interface method.\r\n", sw.ToString()); } } [Test] public void TestExplicitCall() { var implementation = new SomeImplementation() as ISomeInterface; using (StringWriter sw = new StringWriter()) { Console.SetOut(sw); implementation.ExplicitImplementation(); Assert.AreEqual("Call of explicitly implemented interface method.\r\n", sw.ToString()); } } } } This should be solvable using the following method to access all interface members, not just the publicly available ones: [File: InterfaceMembersExtension.cs] using System; using System.Collections.Generic; using System.Reflection; namespace PythonInterfaceCalls { public static class InterfaceMembersExtension { public static IList<MethodInfo> GetImplementedMethods(this Type targetType, Type interfaceType) { return targetType.GetInterfaceMap(interfaceType).TargetMethods; } } } Please find the complete solution attached to this mail. Is there a chance that this will be implemented in PythonNet? Thanks in advance, Hennnig
Hi Henning, the best way to get a change or fix into the pythonnet is to submit it as a pull request to the github repo, along with a description of the problem the PR solves and appropriate unit tests. The github repo can be found here: https://github.com/pythonnet/pythonnet Kind regards, Tony
Hi Tony, This is not a solution for the change but only an idea. I don’t know the concept of PythonNet well enough to figure out where to add the proposed change. And I don’t have a proper development environment to integrate and test the change. That’s why I try to advertise it in the mailing list. I’d be ready to contribute, though. Could you point me to resources on how to debug the Python.Runtime.dll? Best regards, Henning
Hi Henning, You can build, debug and develop Python.Runtime.DLL right from one place - Visual Studio. Free editions or community version (if you qualify) will let you do most of the things. The added benefit is that you can also use Python Tools for Visual Studio as your IDE for Python. Cheers, Denis On Wed, May 25, 2016 at 10:57 AM, Henning Moeller <HMoeller@comprion.com> wrote:
Hi Tony,
This is not a solution for the change but only an idea. I don’t know the concept of PythonNet well enough to figure out where to add the proposed change. And I don’t have a proper development environment to integrate and test the change. That’s why I try to advertise it in the mailing list.
I’d be ready to contribute, though. Could you point me to resources on how to debug the Python.Runtime.dll?
Best regards,
Henning
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org https://mail.python.org/mailman/listinfo/pythondotnet
Please open an issue or two in github :) But I looked through the code and do not see any python or pythonnet calls. Can you describe the problem from the pythonnet side? Did you find that explicit interfaces do not work? Anyway, I learned something new today. On Wed, May 25, 2016 at 7:33 AM, Henning Moeller <HMoeller@comprion.com> wrote:
Hello out there,
I’ve come across an issue with calling a method of an instance which ends up being an explicitly implemented interface method. Let me explain with a very brief example:
Consider this a very basic interface:
[File: ISomeInterface.cs]
namespace PythonInterfaceCalls
{
public interface ISomeInterface
{
void ImplicitImplementation();
void ExplicitImplementation();
}
}
And here’s an implementing class. Note one of the methods being implicitly implemented, the other being explicitly implemented:
[File: SomeImplementation.cs]
using System;
namespace PythonInterfaceCalls
{
public class SomeImplementation: ISomeInterface
{
public void ImplicitImplementation()
{
Console.WriteLine("Call of implicitly implemented interface method.");
}
void ISomeInterface.ExplicitImplementation()
{
Console.WriteLine("Call of explicitly implemented interface method.");
}
}
}
Note the method headers. While the implicit implementation is (and has to be) public, the explicit implementation is (and has to be) internal. Both methods work well in C# (as long as the instance is casted into the interface type). See the unit tests, both passing:
[File: TestImplementation.cs]
using System;
using System.IO;
using NUnit.Framework;
namespace PythonInterfaceCalls
{
[TestFixture]
public class TestImplementation
{
[Test]
public void TestImplicitCall()
{
var implementation = new SomeImplementation() as ISomeInterface;
using (StringWriter sw = new StringWriter())
{
Console.SetOut(sw);
implementation.ImplicitImplementation();
Assert.AreEqual("Call of implicitly implemented interface method.\r\n", sw.ToString());
}
}
[Test]
public void TestExplicitCall()
{
var implementation = new SomeImplementation() as ISomeInterface;
using (StringWriter sw = new StringWriter())
{
Console.SetOut(sw);
implementation.ExplicitImplementation();
Assert.AreEqual("Call of explicitly implemented interface method.\r\n", sw.ToString());
}
}
}
}
This should be solvable using the following method to access all interface members, not just the publicly available ones:
[File: InterfaceMembersExtension.cs]
using System;
using System.Collections.Generic;
using System.Reflection;
namespace PythonInterfaceCalls
{
public static class InterfaceMembersExtension
{
public static IList<MethodInfo> GetImplementedMethods(this Type targetType, Type interfaceType)
{
return targetType.GetInterfaceMap(interfaceType).TargetMethods;
}
}
}
Please find the complete solution attached to this mail.
Is there a chance that this will be implemented in PythonNet?
Thanks in advance,
Hennnig
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org https://mail.python.org/mailman/listinfo/pythondotnet
participants (3)
-
Denis Akhiyarov -
Henning Moeller -
Tony Roberts