diff options
author | Atsushi Kanamori <AtsushiKan@users.noreply.github.com> | 2018-02-09 17:52:01 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-09 17:52:01 +0300 |
commit | 69b71c29533e8cabc7e688aa642fc709fc1986ff (patch) | |
tree | a65851a0c23983174fd577d330dd96daa7979603 /src/System.Private.Reflection.Core | |
parent | c9bd0c49c44ed66b1bef4ca9470e9aaf42f04661 (diff) |
Make some edge-case GetCustomAttribute() behavior match NETFx behavior. (#5368)
This was discovered while reviewing incoming CoreCLR changes
that enable the use of generic custom attributes. With these
changes (plus a separate change that comments out an assert in NUTC)
the tests introduced for that feature pass in Project N.
Object[] ICustomAttributeProvider.GetCustomAttributes()
If the passed in attribute type is an open type or a value type,
the return array is of type Object[] rather than the exact
array type (which would be impossible in the case of an open type.)
Chances are the array length would be zero though I don't
hardcode that assumption.
IEnumerable<Attribute> CustomAttributeExtensions.GetCustomAttributes()
If the passed in attribute type is an open type, the code internally
generates a Object[] array which then decays to null due to a
"as Attribute[]" conversion.
If the passed in attribute type is a value type, it gets thrown
out by the "must derive from System.Attribute" check so that
case doesn't apply to this api.
Attribute.GetCustomAttributes()
Everything that applies to CustomAttributeExtensions applies
to this.
Diffstat (limited to 'src/System.Private.Reflection.Core')
-rw-r--r-- | src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Helpers.cs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Helpers.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Helpers.cs index 90943008c..5d851fb09 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Helpers.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Helpers.cs @@ -189,8 +189,14 @@ namespace System.Reflection.Runtime.General object instantiatedAttribute = cad.Instantiate(); attributes.Add(instantiatedAttribute); } + + // This is here for desktop compatibility. ICustomAttribute.GetCustomAttributes() normally returns an array of the + // exact attribute type requested except in two cases: when the passed in type is an open type and when + // it is a value type. In these two cases, it returns an array of type Object[]. + bool useObjectArray = actualElementType.ContainsGenericParameters || actualElementType.IsValueType; int count = attributes.Count; - object[] result = (object[])Array.CreateInstance(actualElementType, count); + object[] result = useObjectArray ? new object[count] : (object[])Array.CreateInstance(actualElementType, count); + attributes.CopyTo(result, 0); return result; } |