-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom attribute properties were not always applied correctly (#44813)
- Loading branch information
1 parent
0897d4a
commit 63923b5
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
80 changes: 80 additions & 0 deletions
80
src/libraries/System.Reflection/tests/CustomAttributeTests.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace System.Reflection.Tests | ||
{ | ||
public class CustomAttributeTests | ||
{ | ||
private class SameTypesAttribute : Attribute | ||
{ | ||
public object[] ObjectArray1 { get; set; } | ||
public object[] ObjectArray2 { get; set; } | ||
} | ||
|
||
[SameTypes(ObjectArray1 = null, ObjectArray2 = new object[] { "" })] | ||
private class SameTypesClass1 { } | ||
|
||
[SameTypes(ObjectArray1 = new object[] { "" }, ObjectArray2 = null)] | ||
private class SameTypesClass2 { } | ||
|
||
[Fact] | ||
public void AttributeWithSamePropertyTypes() | ||
{ | ||
SameTypesAttribute attr; | ||
|
||
attr = typeof(SameTypesClass1) | ||
.GetCustomAttributes(typeof(SameTypesAttribute), true) | ||
.Cast<SameTypesAttribute>() | ||
.Single(); | ||
|
||
Assert.Null(attr.ObjectArray1); | ||
Assert.Equal(1, attr.ObjectArray2.Length); | ||
|
||
attr = typeof(SameTypesClass2) | ||
.GetCustomAttributes(typeof(SameTypesAttribute), true) | ||
.Cast<SameTypesAttribute>() | ||
.Single(); | ||
|
||
Assert.Equal(1, attr.ObjectArray1.Length); | ||
Assert.Null(attr.ObjectArray2); | ||
} | ||
|
||
private class DifferentTypesAttribute : Attribute | ||
{ | ||
public object[] ObjectArray { get; set; } | ||
public string[] StringArray { get; set; } | ||
} | ||
|
||
[DifferentTypes(ObjectArray = null, StringArray = new[] { "" })] | ||
private class DifferentTypesClass1 { } | ||
|
||
[DifferentTypes(ObjectArray = new object[] { "" }, StringArray = null)] | ||
private class DifferentTypesClass2 { } | ||
|
||
[Fact] | ||
public void AttributeWithDifferentPropertyTypes() | ||
{ | ||
DifferentTypesAttribute attr; | ||
|
||
attr = typeof(DifferentTypesClass1) | ||
.GetCustomAttributes(typeof(DifferentTypesAttribute), true) | ||
.Cast<DifferentTypesAttribute>() | ||
.Single(); | ||
|
||
Assert.Null(attr.ObjectArray); | ||
Assert.Equal(1, attr.StringArray.Length); | ||
|
||
attr = typeof(DifferentTypesClass2) | ||
.GetCustomAttributes(typeof(DifferentTypesAttribute), true) | ||
.Cast<DifferentTypesAttribute>() | ||
.Single(); | ||
|
||
Assert.Equal(1, attr.ObjectArray.Length); | ||
Assert.Null(attr.StringArray); | ||
} | ||
} | ||
} |
This file contains 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