Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
throw NotSupportedException when reqesting open generic attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
AviAvni committed Feb 8, 2018
1 parent 11c28a8 commit 69603dc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/mscorlib/src/System/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ public static Attribute[] GetCustomAttributes(MemberInfo element, Type type, boo
if (type == null)
throw new ArgumentNullException(nameof(type));

if (type.IsGenericTypeDefinition)
throw new NotSupportedException(nameof(type));

if (!type.IsSubclassOf(typeof(Attribute)) && type != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
Contract.EndContractBlock();
Expand Down Expand Up @@ -570,6 +573,9 @@ public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attrib
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

if (attributeType.IsGenericTypeDefinition)
throw new NotSupportedException(nameof(attributeType));

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);

Expand Down Expand Up @@ -692,6 +698,9 @@ public static Attribute[] GetCustomAttributes(Module element, Type attributeType
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

if (attributeType.IsGenericTypeDefinition)
throw new NotSupportedException(nameof(attributeType));

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
Contract.EndContractBlock();
Expand Down Expand Up @@ -756,6 +765,9 @@ public static Attribute[] GetCustomAttributes(Assembly element, Type attributeTy
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

if (attributeType.IsGenericTypeDefinition)
throw new NotSupportedException(nameof(attributeType));

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
Contract.EndContractBlock();
Expand Down
24 changes: 22 additions & 2 deletions tests/src/reflection/GenericAttribute/GenericAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Program
{
static int Main(string[] args)
{
new MultiAttribute<string>("a");
Assembly assembly = typeof(Class).GetTypeInfo().Assembly;
Assert(CustomAttributeExtensions.GetCustomAttribute<SingleAttribute<int>>(assembly) != null);
Assert(((ICustomAttributeProvider)assembly).GetCustomAttributes(typeof(SingleAttribute<int>), true) != null);
Expand Down Expand Up @@ -133,8 +134,9 @@ static int Main(string[] args)
var b8 = ((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute<bool?>), true);
AssertAny(b8, a => (a as MultiAttribute<bool?>)?.Value == null);

Assert(CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, typeof(MultiAttribute<>), false) == null);
Assert(CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, typeof(MultiAttribute<>), true) == null);
AssertThrow<NotSupportedException>(() => CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, typeof(MultiAttribute<>), false));
AssertThrow<NotSupportedException>(() => CustomAttributeExtensions.GetCustomAttributes(programTypeInfo, typeof(MultiAttribute<>), true));

Assert(!((ICustomAttributeProvider)programTypeInfo).GetCustomAttributes(typeof(MultiAttribute<>), true).GetEnumerator().MoveNext());

return 100;
Expand All @@ -148,6 +150,24 @@ static void Assert(bool condition, [CallerLineNumberAttribute]int line = 0)
}
}

static void AssertThrow<T>(Action action, [CallerLineNumberAttribute]int line = 0)
where T : Exception
{
try
{
action();
throw new Exception($"Error in line: {line}");
}
catch (T)
{

}
catch (Exception)
{
throw new Exception($"Error in line: {line}");
}
}

static void AssertAny(IEnumerable<object> source, Func<Attribute, bool> condition, int count = 1, [CallerLineNumberAttribute]int line = 0)
{
var enumerator = source.GetEnumerator();
Expand Down

0 comments on commit 69603dc

Please sign in to comment.