Skip to content
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

System.Reflection.CustomAttributeFormatException thrown on a retrieval of derived attribute with overridden property getter #110945

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

pedrobsaila
Copy link
Contributor

Fixes #110412

…l of derived attribute with overridden property getter
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Dec 25, 2024
@@ -1580,7 +1580,7 @@ private static void AddCustomAttributes(
RuntimeMethodInfo setMethod = property.GetSetMethod(true)!;

// Public properties may have non-public setter methods
if (!setMethod.IsPublic)
if (setMethod == null || !setMethod.IsPublic)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is null to avoid unnecessary comparison operator

@@ -1580,7 +1580,7 @@ private static void AddCustomAttributes(
RuntimeMethodInfo setMethod = property.GetSetMethod(true)!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nullability suppression should also be removed

Type? baseDeclaredType = declaredType.BaseType;

while ((m_getterMethod == null || m_setterMethod == null)
&& baseDeclaredType != null && baseDeclaredType is RuntimeType baseDeclaredRuntimeType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&& baseDeclaredType != null && baseDeclaredType is RuntimeType baseDeclaredRuntimeType)
&& baseDeclaredType is RuntimeType baseDeclaredRuntimeType)

The pattern match also does a null check.

Copy link
Member

@steveharter steveharter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some test failures for Mono; the changes need to be ported to Mono as well.

out _, out _, out _,
out m_getterMethod, out m_setterMethod, out m_otherMethod,
out isPrivate, out m_bindingFlags);

// lookup getter/setter when getter and setter are inherited from base class but just a setter/getter is overridden on a sub type
if ((m_getterMethod != null && m_getterMethod.IsVirtual && m_setterMethod == null)
Copy link
Member

@steveharter steveharter Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: as mentioned earlier, is null and is not null is preferred over == null and != null since it will prevent a call to the == or != operator if that is overridden. In this case, RuntimeMethodInfo doesn't overload those operators so it doesn't matter but it is just more obvious to understand intent when using is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuntimeMethodInfo doesn't overload those operators so it doesn't matter

MethodInfo does overload those operators. MethodInfo operators are going to be selected to compare RuntimeMethodInfo quick test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops thanks; updated comment

out _, out _, out _,
out m_getterMethod, out m_setterMethod, out m_otherMethod,
out isPrivate, out m_bindingFlags);

// lookup getter/setter when getter and setter are inherited from base class but just a setter/getter is overridden on a sub type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: start a comment with upper case letter and end with a period.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Reflection community-contribution Indicates that the PR has been added by a community member
Projects
None yet
5 participants