Skip to content

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

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
spynetcamera opened this issue May 13, 2021 · 0 comments · Fixed by #1650
Labels
Milestone

Comments

@spynetcamera
Copy link

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.
@lostmsu lostmsu added this to the 3.0.0 milestone Sep 23, 2021
@lostmsu lostmsu added the bug label Oct 1, 2021
lostmsu added a commit to losttech/pythonnet that referenced this issue Dec 28, 2021
…etMethod would not return base non-overriden accessor for a partially overriden property

because of that when constructing PropertyObject we scan base classes to find base accessor (if any)

this might have performance implications due to replacement of PropertyInfo.GetValue with getter.Invoke (not tested)

fixes pythonnet#1455
lostmsu added a commit to losttech/pythonnet that referenced this issue Dec 28, 2021
…etMethod would not return base non-overriden accessor for a partially overriden property

because of that when constructing PropertyObject we scan base classes to find base accessor (if any)

this might have performance implications due to replacement of PropertyInfo.GetValue with getter.Invoke (not tested)

fixes pythonnet#1455
lostmsu added a commit to losttech/pythonnet that referenced this issue Dec 29, 2021
…etMethod would not return base non-overriden accessor for a partially overriden property

because of that when constructing PropertyObject we scan base classes to find base accessor (if any)

this might have performance implications due to replacement of PropertyInfo.GetValue with getter.Invoke (not tested)

fixes pythonnet#1455
lostmsu added a commit to losttech/pythonnet that referenced this issue Dec 31, 2021
…etMethod would not return base non-overriden accessor for a partially overriden property

because of that when constructing PropertyObject we scan base classes to find base accessor (if any)

this might have performance implications due to replacement of PropertyInfo.GetValue with getter.Invoke (not tested)

fixes pythonnet#1455
lostmsu added a commit that referenced this issue Jan 4, 2022
…etMethod would not return base non-overriden accessor for a partially overriden property

because of that when constructing PropertyObject we scan base classes to find base accessor (if any)

this might have performance implications due to replacement of PropertyInfo.GetValue with getter.Invoke (not tested)

fixes #1455
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants