Skip to content

Commit 6d224ce

Browse files
committed
[mono] Don't throw inheritance error on interfaces in GetCustomAttrs
Looking at #7190, the .NET Core behavior here is strange and my changes in 0844cb7a6152 with the type checking may have been a mistake. I think this should be fine with the interface special-casing, but if deemed too great a concern I can revert that subset of my old changes. Additionally, if people come up with other scenarios where this might fail, I'll just revert until we can migrate to use a shared CustomAttribute implementation rather than try to play whack-a-mole.
1 parent dcbee66 commit 6d224ce

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/libraries/System.Runtime/tests/System/Type/TypeTests.Get.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public void GetInterface_MixedCaseAmbiguity_ThrowsAmbiguousMatchException()
9191
{
9292
Assert.Throws<AmbiguousMatchException>(() => typeof(ClassWithMixedCaseInterfaces).GetInterface("mixedinterface", ignoreCase: true));
9393
}
94+
95+
[Fact]
96+
public void GetCustomAttributes_Interface()
97+
{
98+
Assert.True(typeof(ExampleWithAttribute).GetCustomAttributes(typeof(INameable), inherit: false)[0] is NameableAttribute);
99+
}
94100
}
95101

96102
public class ClassWithNoInterfaces { }
@@ -116,6 +122,20 @@ public interface Interface1 { }
116122
public interface Interface2 { }
117123
public interface Interface3 { }
118124
}
125+
126+
public interface INameable
127+
{
128+
string Name { get; }
129+
}
130+
131+
[AttributeUsage (AttributeTargets.All, AllowMultiple=true)]
132+
public class NameableAttribute : Attribute, INameable
133+
{
134+
string INameable.Name => "Nameable";
135+
}
136+
137+
[Nameable]
138+
public class ExampleWithAttribute { }
119139
}
120140

121141
public interface Interface1 { }

src/mono/netcore/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ internal static object[] GetCustomAttributes(ICustomAttributeProvider obj, Type
140140
throw new ArgumentNullException(nameof(obj));
141141
if (attributeType == null)
142142
throw new ArgumentNullException(nameof(attributeType));
143-
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute) && attributeType != typeof(CustomAttribute) && attributeType != typeof(object))
143+
if (!attributeType.IsSubclassOf(typeof(Attribute)) && !attributeType.IsInterface
144+
&& attributeType != typeof(Attribute) && attributeType != typeof(CustomAttribute) && attributeType != typeof(object))
144145
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);
145146

146147
if (attributeType == typeof(CustomAttribute))

0 commit comments

Comments
 (0)