Visitar URL original
.NET property not accessible in derived classes where only the setter or the getter has been overridden · Issue #1455 · pythonnet/pythonnet · GitHub
Skip to content

.NET property not accessible in derived classes where only the setter or the getter has been overridden #1455

Description

@spynetcamera

Environment

  • Pythonnet version: 2.5.2
  • Python version: 3.7
  • Operating System: Windows 10
  • .NET Runtime: 4.8

Details

  • Let's create a simple base class that exposes a property, then create a derived class that overrides the setter, but not the getter (that is supposed to be as it was defined in the base class)
namespace PythonNetTest
{
    public class BaseClass
    {
        protected string name = "noname";

        public virtual string Name { get => name; set => name = value; }
    }

    public class DerivedClass : BaseClass
    {
        public override string Name { set => base.Name = value.ToUpper(); }
    }
}
  • Let's now use the above classes in Python
import clr
clr.AddReference("PythonNetTest")
from PythonNetTest import BaseClass, DerivedClass

d = DerivedClass()
b = BaseClass()

b.Name = 'BaseClass Name'
d.Name = 'DerivedClass Name'

print(b.Name)   # ok
print(d.Name)   # TypeError: property cannot be read
  • Here the print(d.Name) will rise the exception TypeError: property cannot be read.
  • As a workaround it is possible to use print(d.GetType().BaseType.GetProperty('Name').GetValue(d)) instead, but it is not one would expect.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions