8000 Explicit interface implementations by danabr · Pull Request #1233 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Explicit interface implementations #1233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Expose methods/props of explicitly implemented interfaces to Python
Previously, if a class implemented an interface explicitly, those
methods and properties become invisible to Python. To use them, you had
to make an explicit cast to the interface type.
  • Loading branch information
danabr committed Sep 23, 2020
commit 40eac9365be7fe72afae4d8c200f514c57c7d3cf
43 changes: 20 additions & 23 deletions src/runtime/classmanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,32 +241,29 @@ private static ClassInfo GetClassInfo(Type type)
}
}

if (type.IsInterface)
// Interface inheritance seems to be a different animal:
// more contractual, less structural. Thus, a Type that
// represents an interface that inherits from another
// interface does not return the inherited interface's
// methods in GetMembers. For example ICollection inherits
// from IEnumerable, but ICollection's GetMemebers does not
// return GetEnumerator.
//
// Not sure if this is the correct way to fix this, but it
// seems to work. Thanks to Bruce Dodson for the fix.

Type[] inheritedInterfaces = type.GetInterfaces();

for (i = 0; i < inheritedInterfaces.Length; ++i)
{
// Interface inheritance seems to be a different animal:
// more contractual, less structural. Thus, a Type that
// represents an interface that inherits from another
// interface does not return the inherited interface's
// methods in GetMembers. For example ICollection inherits
// from IEnumerable, but ICollection's GetMemebers does not
// return GetEnumerator.
//
// Not sure if this is the correct way to fix this, but it
// seems to work. Thanks to Bruce Dodson for the fix.

Type[] inheritedInterfaces = type.GetInterfaces();

for (i = 0; i < inheritedInterfaces.Length; ++i)
Type inheritedType = inheritedInterfaces[i];
MemberInfo[] imembers = inheritedType.GetMembers(flags);
for (n = 0; n < imembers.Length; n++)
{
Type inheritedType = inheritedInterfaces[i];
MemberInfo[] imembers = inheritedType.GetMembers(flags);
for (n = 0; n < imembers.Length; n++)
m = imembers[n];
if (local[m.Name] == null)
{
m = imembers[n];
if (local[m.Name] == null)
{
items.Add(m);
}
items.Add(m);
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion src/testing/interfacetest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ public interface ISayHello2
string SayHello();
}

public class InterfaceTest : ISayHello1, ISayHello2
public interface IInterfaceResolutionTest
{
string TestMethod1();
string TestMethod2();

string TestProperty1 { get; }
string TestProperty2 { get; }
}

public class InterfaceTest : ISayHello1, ISayHello2, IInterfaceResolutionTest
{
public InterfaceTest()
{
Expand All @@ -43,6 +52,20 @@ string ISayHello2.SayHello()
return "hello 2";
}

public string TestMethod1()
{
return "TestMethod1";
}

string IInterfaceResolutionTest.TestMethod2()
{
return "TestMethod2";
}

public string TestProperty1 => "TestProperty1";

string IInterfaceResolutionTest.TestProperty2 => "TestProperty2";

public interface IPublic
{
}
Expand Down
16 changes: 16 additions & 0 deletions src/tests/test_interface.py
38BF
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,19 @@ def test_explicit_cast_to_interface():
assert i2.SayHello() == 'hello 2'
assert hasattr(i2, 'SayHello')
assert not hasattr(i2, 'HelloProperty')


def test_interface_method_and_property_lookup():
"""Test methods and properties in interfaces can be accessed"""
from Python.Test import InterfaceTest

ob = InterfaceTest()
assert hasattr(ob, 'TestMethod1')
assert ob.TestMethod1() == 'TestMethod1'
assert hasattr(ob, 'TestMethod2')
assert ob.TestMethod2() == 'TestMethod2'
assert hasattr(ob, 'TestProperty1')
assert ob.TestProperty1 == 'TestProperty1'
assert hasattr(ob, 'TestProperty2')
assert ob.TestProperty2 == 'TestProperty2'

0