dotnet / dotnet/dotnet-api-docs

Document of PropertyInfo didn't say it won't treat private getter/setter as readable/writable when access by an subType.

Open
#4,837 1 comment 0 reactions 0 assignees View on GitHub
area-System.Reflection Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

### Description

Document of PropertyInfo didn't say it won't treat private getter/setter as readable/writable when access by an subType, and `noPublic` is still not include private accessor from baseType.
It just say `CanRead`/`CanWrite` is `false` without an getter/setter, and `SetMethod` say it's equal to `GetSetMethod(nonPublic: true)` and so it can return noPublic method.
https://docs.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setmethod?view=netcore-3.1
When GetProperty is called on the same type, the doc is right. But it won't work when GetProperty is called on an subType , and see the below code for details.

### Configuration

Tested, and the same behavior in net472/netcoreapp2.1/3.1/net5.0preview4

### Regression?

No, and infact there is even an test to verify the current behavior(see `typeof(PI_SubClass), nameof(PI_BaseClass.PublicGetPrivateSetProperty)`);
https://github.com/dotnet/runtime/blob/b74422f2d528937830ae50b241aeb3d868979228/src/libraries/System.Reflection.TypeExtensions/tests/PropertyInfoTests.cs#L27-L64

### Other information

```cs

public class X
{
public int Value { get; private set; }
}

public class Y : X { }

foreach (var prop in typeof(X).GetProperties(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
Console.WriteLine($"{prop.ReflectedType}>{prop.DeclaringType}.{prop.Name}:r={prop.CanRead},{prop.GetGetMethod(true) != null}, w={prop.CanWrite},{prop.GetSetMethod(true) != null}");//X>X.Value:r=true,true, w= true,ture
}
foreach (var prop in typeof(Y).GetProperties(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
Console.WriteLine($"{prop.ReflectedType}>{prop.DeclaringType}.{prop.Name}:r={prop.CanRead},{prop.GetGetMethod(true) != null}, w={prop.CanWrite},{prop.GetSetMethod(true) != null}");//Y>X.Value:r=false,false, w= true,ture
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.