.NET property not accessible in derived classes where only the setter or the getter has been overridden
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 exceptionTypeError: 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.