Skip to content

Commit

Permalink
[mono] Don't throw inheritance error on interfaces in GetCustomAttrs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CoffeeFlux committed Mar 25, 2020
1 parent dcbee66 commit 6d224ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/libraries/System.Runtime/tests/System/Type/TypeTests.Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public void GetInterface_MixedCaseAmbiguity_ThrowsAmbiguousMatchException()
{
Assert.Throws<AmbiguousMatchException>(() => typeof(ClassWithMixedCaseInterfaces).GetInterface("mixedinterface", ignoreCase: true));
}

[Fact]
public void GetCustomAttributes_Interface()
{
Assert.True(typeof(ExampleWithAttribute).GetCustomAttributes(typeof(INameable), inherit: false)[0] is NameableAttribute);
}
}

public class ClassWithNoInterfaces { }
Expand All @@ -116,6 +122,20 @@ public interface Interface1 { }
public interface Interface2 { }
public interface Interface3 { }
}

public interface INameable
{
string Name { get; }
}

[AttributeUsage (AttributeTargets.All, AllowMultiple=true)]
public class NameableAttribute : Attribute, INameable
{
string INameable.Name => "Nameable";
}

[Nameable]
public class ExampleWithAttribute { }
}

public interface Interface1 { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ internal static object[] GetCustomAttributes(ICustomAttributeProvider obj, Type
throw new ArgumentNullException(nameof(obj));
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute) && attributeType != typeof(CustomAttribute) && attributeType != typeof(object))
if (!attributeType.IsSubclassOf(typeof(Attribute)) && !attributeType.IsInterface
&& attributeType != typeof(Attribute) && attributeType != typeof(CustomAttribute) && attributeType != typeof(object))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);

if (attributeType == typeof(CustomAttribute))
Expand Down

0 comments on commit 6d224ce

Please sign in to comment.