dotnet / dotnet/dotnet-api-docs

PropertyInfo.IsDefined doesn't respect inherit=true

Open
#8,590 7 comments 0 reactions 0 assignees View on GitHub
area-System.Reflection documentation Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

### Description

Calling `PropertyInfo.IsDefined(type, inherit: true)` doesn't work as expected.

```C#
class Base
{
[Description("my description")]
public virtual string Name { get; set; }
}
class Derived : Base
{
public override string Name { get => base.Name; set => base.Name = value; }
}

static int Main(string[] args)
{
var pi = typeof(Derived).GetProperty("Name");

Console.WriteLine(pi.IsDefined(typeof(DescriptionAttribute), inherit: false)); // prints `False` as expected
Console.WriteLine(pi.IsDefined(typeof(DescriptionAttribute), inherit: true)); // BUG - this prints `False`

Console.WriteLine(pi.IsDefined(typeof(DescriptionAttribute))); // prints `True` as expected
Console.WriteLine(CustomAttributeExtensions.IsDefined(pi, typeof(DescriptionAttribute), inherit: false)); // prints `False` as expected
Console.WriteLine(CustomAttributeExtensions.IsDefined(pi, typeof(DescriptionAttribute), inherit: true)); // prints `True` as expected
}
```

### Configuration

* Which version of .NET is the code running on? .NET 5
* What OS and version, and what distro if applicable? windows
* What is the architecture (x64, x86, ARM, ARM64)? x64
* Do you know whether it is specific to that configuration? It is not.

### Regression?

No. Tried the above code on .NET Framework 4.8 and it appears to have the same behavior as .NET 5.

### Other information

https://github.com/dotnet/runtime/blob/8636dc8a76d687169db64033b71ec9f79d9ba4b5/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs#L153-L164

That code doesn't use the `inherit` parameter.

This may be a problem in other Reflection APIs, we should do an inspection of other APIs that may have this same problem.

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.