-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Custom attribute properties were not always applied correctly #44813
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Should this be moved above the switch statement to make this code more future-proof should new case statements be added in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was considered, but there's the tiny cost of double assignment.