From 27a870e25eb903eca6cd096dd40924184ec52669 Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Tue, 17 Nov 2020 11:37:57 -0600 Subject: [PATCH] Custom attribute properties were not always applied correctly --- src/coreclr/src/vm/customattribute.cpp | 2 +- .../tests/CustomAttributeTests.cs | 80 +++++++++++++++++++ .../tests/System.Reflection.Tests.csproj | 1 + 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/libraries/System.Reflection/tests/CustomAttributeTests.cs diff --git a/src/coreclr/src/vm/customattribute.cpp b/src/coreclr/src/vm/customattribute.cpp index 3dea5604587b9..e0fd928721192 100644 --- a/src/coreclr/src/vm/customattribute.cpp +++ b/src/coreclr/src/vm/customattribute.cpp @@ -908,7 +908,6 @@ FCIMPL7(void, COMCustomAttribute::GetPropertyOrFieldData, ReflectModuleBaseObjec nullTH = th; } - // // get the string representing the field/property name *pName = ArgSlotToString(GetDataFromBlob( pCtorAssembly, SERIALIZATION_TYPE_STRING, nullTH, &pBlob, pBlobEnd, pModule, &bObjectCreated)); @@ -937,6 +936,7 @@ FCIMPL7(void, COMCustomAttribute::GetPropertyOrFieldData, ReflectModuleBaseObjec break; case SERIALIZATION_TYPE_SZARRAY: { + *value = NULL; int arraySize = (int)GetDataFromBlob(pCtorAssembly, SERIALIZATION_TYPE_I4, nullTH, &pBlob, pBlobEnd, pModule, &bObjectCreated); if (arraySize != -1) diff --git a/src/libraries/System.Reflection/tests/CustomAttributeTests.cs b/src/libraries/System.Reflection/tests/CustomAttributeTests.cs new file mode 100644 index 0000000000000..bb9c928b54220 --- /dev/null +++ b/src/libraries/System.Reflection/tests/CustomAttributeTests.cs @@ -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() + .Single(); + + Assert.Null(attr.ObjectArray1); + Assert.Equal(1, attr.ObjectArray2.Length); + + attr = typeof(SameTypesClass2) + .GetCustomAttributes(typeof(SameTypesAttribute), true) + .Cast() + .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() + .Single(); + + Assert.Null(attr.ObjectArray); + Assert.Equal(1, attr.StringArray.Length); + + attr = typeof(DifferentTypesClass2) + .GetCustomAttributes(typeof(DifferentTypesAttribute), true) + .Cast() + .Single(); + + Assert.Equal(1, attr.ObjectArray.Length); + Assert.Null(attr.StringArray); + } + } +} diff --git a/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj b/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj index f480b9783c53d..5b4ff27858395 100644 --- a/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj +++ b/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj @@ -17,6 +17,7 @@ +