Skip to content

Commit ce2242f

Browse files
committed
Add test that GetCustomAttribute returns string.Empty
And not a new string object that happens to be `""` Adds a testcase for dotnet#58023
1 parent 5c47007 commit ce2242f

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

src/libraries/System.Reflection/tests/CustomAttributeTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,58 @@ public void AttributeWithDifferentPropertyTypes()
7676
Assert.Equal(1, attr.ObjectArray.Length);
7777
Assert.Null(attr.StringArray);
7878
}
79+
80+
public class StringValuedAttribute : Attribute
81+
{
82+
public StringValuedAttribute (string s)
83+
{
84+
NamedField = s;
85+
}
86+
public StringValuedAttribute () {}
87+
public string NamedProperty
88+
{
89+
get => NamedField;
90+
set { NamedField = value; }
91+
}
92+
public string NamedField;
93+
}
94+
95+
internal class ClassWithAttrs
96+
{
97+
[StringValuedAttribute("")]
98+
public void M1() {}
99+
100+
[StringValuedAttribute(NamedProperty = "")]
101+
public void M2() {}
102+
103+
[StringValuedAttribute(NamedField = "")]
104+
public void M3() {}
105+
}
106+
107+
[Fact]
108+
public void StringAttributeValueRefEqualsStringEmpty () {
109+
StringValuedAttribute attr;
110+
attr = typeof (ClassWithAttrs).GetMethod("M1")
111+
.GetCustomAttributes(typeof(StringValuedAttribute), true)
112+
.Cast<StringValuedAttribute>()
113+
.Single();
114+
115+
Assert.Same(string.Empty, attr.NamedField);
116+
117+
attr = typeof (ClassWithAttrs).GetMethod("M2")
118+
.GetCustomAttributes(typeof(StringValuedAttribute), true)
119+
.Cast<StringValuedAttribute>()
120+
.Single();
121+
122+
Assert.Same(string.Empty, attr.NamedField);
123+
124+
125+
attr = typeof (ClassWithAttrs).GetMethod("M3")
126+
.GetCustomAttributes(typeof(StringValuedAttribute), true)
127+
.Cast<StringValuedAttribute>()
128+
.Single();
129+
130+
Assert.Same(string.Empty, attr.NamedField);
131+
}
79132
}
80133
}

0 commit comments

Comments
 (0)