Preserve attribute inheritance behavior in the MA0179 code fix - #1457
Merged
Merged
Conversation
The instance MemberInfo.GetCustomAttributes(Type, bool) and MemberInfo.GetCustomAttributes(bool) methods ignore 'inherit' for properties and events, while Attribute.IsDefined(MemberInfo, Type, bool) walks the chain of overridden properties and events. Copying the 'inherit' argument into Attribute.IsDefined could therefore turn a false result into true. The code fix now uses the instance MemberInfo.IsDefined(Type, bool) when the receiver can be a PropertyInfo or EventInfo and 'inherit' is not the constant false. Other cases still use Attribute.IsDefined.
This was referenced Sep 11, 2026
Closed
This was referenced Sep 17, 2026
Merged
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The MA0179 code fix replaced instance
GetCustomAttributescalls withAttribute.IsDefinedand copied theinheritargument unchanged. The two APIs don't handleinheritthe same way on properties and events:MemberInfo.GetCustomAttributes(Type, bool)andMemberInfo.GetCustomAttributes(bool)methods ignoreinheritfor properties and eventsAttribute.IsDefined(MemberInfo, Type, bool)also searches the overridden properties and events wheninheritistrueI ran this on .NET 10 for each member kind:
GetCustomAttributes(t, true)IsDefined(t, true)Attribute.IsDefined(m, t, true)The extension (
CustomAttributeExtensions) and staticAttribute.GetCustomAttribute(s)calls were not affected: they already search inherited attributes the same way asAttribute.IsDefined.Fix
For instance
GetCustomAttributescalls, the fix now produces the instanceMemberInfo.IsDefined(Type, bool)when:PropertyInfoorEventInfoat runtime (for exampleMemberInfo,PropertyInfoorEventInfo), andinheritis not the constantfalse.MemberInfo.IsDefined(Type, bool)gives the same result as the original call. In all other cases (inherit: false, receivers such asType,MethodInfo,FieldInfo,Assembly,Module, and the extension and static calls), the fix still producesAttribute.IsDefined.Tests
Attribute.IsDefined(member, ..., inherit: true)for aMemberInforeceiver, which is the buggy output. They now expect the instanceIsDefined.PropertyInfoandEventInfowithinherit: trueinheritvariablePropertyInfowithinherit: false, which still getsAttribute.IsDefinedType,MethodInfoandFieldInfowithinherit: true, which still getAttribute.IsDefinedGetCustomAttributes(Type)on aPropertyInfo, which still getsAttribute.IsDefinedUseAttributeIsDefinedAnalyzerTestspass on Roslyn 4.8, 4.14, 5.0, 5.6 and 5.9.dotnet run --project src/DocumentationGeneratormakes no further changes.Notes for reviewers
docs/Rules/MA0179.mdhas a new section describing this behavior.member.IsDefined(...). Showing a different title would mean building the replacement before the fix is registered, which is a larger refactor of the fixer. I left it out of this PR.