Skip to content
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 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/coreclr/src/vm/customattribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -937,6 +936,7 @@ FCIMPL7(void, COMCustomAttribute::GetPropertyOrFieldData, ReflectModuleBaseObjec
break;
case SERIALIZATION_TYPE_SZARRAY:
{
*value = NULL;
Copy link
Member

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?

Copy link
Member Author

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.

int arraySize = (int)GetDataFromBlob(pCtorAssembly, SERIALIZATION_TYPE_I4, nullTH, &pBlob, pBlobEnd, pModule, &bObjectCreated);

if (arraySize != -1)
Expand Down
80 changes: 80 additions & 0 deletions src/libraries/System.Reflection/tests/CustomAttributeTests.cs
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Compile Include="AssemblyNameTests.cs" />
<Compile Include="AssemblyTests.cs" />
<Compile Include="ConstructorInfoTests.cs" />
<Compile Include="CustomAttributeTests.cs" />
<Compile Include="EventInfoTests.cs" />
<Compile Include="FieldInfoTests.cs" />
<Compile Include="GetTypeTests.cs" />
Expand Down