forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomAttributeTests.cs
More file actions
241 lines (190 loc) · 9.22 KB
/
CustomAttributeTests.cs
File metadata and controls
241 lines (190 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// 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);
}
public class StringValuedAttribute : Attribute
{
public StringValuedAttribute (string s)
{
NamedField = s;
}
public StringValuedAttribute () {}
public string NamedProperty
{
get => NamedField;
set { NamedField = value; }
}
public string NamedField;
}
internal class ClassWithAttrs
{
[StringValuedAttribute("")]
public void M1() {}
[StringValuedAttribute(NamedProperty = "")]
public void M2() {}
[StringValuedAttribute(NamedField = "")]
public void M3() {}
}
[Fact]
public void StringAttributeValueRefEqualsStringEmpty () {
StringValuedAttribute attr;
attr = typeof (ClassWithAttrs).GetMethod("M1")
.GetCustomAttributes(typeof(StringValuedAttribute), true)
.Cast<StringValuedAttribute>()
.Single();
Assert.Same(string.Empty, attr.NamedField);
attr = typeof (ClassWithAttrs).GetMethod("M2")
.GetCustomAttributes(typeof(StringValuedAttribute), true)
.Cast<StringValuedAttribute>()
.Single();
Assert.Same(string.Empty, attr.NamedField);
attr = typeof (ClassWithAttrs).GetMethod("M3")
.GetCustomAttributes(typeof(StringValuedAttribute), true)
.Cast<StringValuedAttribute>()
.Single();
Assert.Same(string.Empty, attr.NamedField);
}
[AttributeUsage(AttributeTargets.Parameter)]
internal class MyParameterAttribute : Attribute {}
[AttributeUsage(AttributeTargets.Property)]
internal class MyPropertyAttribute : Attribute {}
internal sealed class PropertyAsParameterInfo : ParameterInfo
{
private readonly PropertyInfo _underlyingProperty;
private readonly ParameterInfo? _constructionParameterInfo;
public PropertyAsParameterInfo(PropertyInfo property, ParameterInfo parameterInfo)
{
_underlyingProperty = property;
_constructionParameterInfo = parameterInfo;
MemberImpl = _underlyingProperty;
}
public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
var constructorAttributes = _constructionParameterInfo?.GetCustomAttributes(attributeType, inherit);
if (constructorAttributes == null || constructorAttributes is { Length: 0 })
{
return _underlyingProperty.GetCustomAttributes(attributeType, inherit);
}
var propertyAttributes = _underlyingProperty.GetCustomAttributes(attributeType, inherit);
var mergedAttributes = new Attribute[constructorAttributes.Length + propertyAttributes.Length];
Array.Copy(constructorAttributes, mergedAttributes, constructorAttributes.Length);
Array.Copy(propertyAttributes, 0, mergedAttributes, constructorAttributes.Length, propertyAttributes.Length);
return mergedAttributes;
}
public override object[] GetCustomAttributes(bool inherit)
{
var constructorAttributes = _constructionParameterInfo?.GetCustomAttributes(inherit);
if (constructorAttributes == null || constructorAttributes is { Length: 0 })
{
return _underlyingProperty.GetCustomAttributes(inherit);
}
var propertyAttributes = _underlyingProperty.GetCustomAttributes(inherit);
var mergedAttributes = new object[constructorAttributes.Length + propertyAttributes.Length];
Array.Copy(constructorAttributes, mergedAttributes, constructorAttributes.Length);
Array.Copy(propertyAttributes, 0, mergedAttributes, constructorAttributes.Length, propertyAttributes.Length);
return mergedAttributes;
}
public override IList<CustomAttributeData> GetCustomAttributesData()
{
var attributes = new List<CustomAttributeData>(
_constructionParameterInfo?.GetCustomAttributesData() ?? Array.Empty<CustomAttributeData>());
attributes.AddRange(_underlyingProperty.GetCustomAttributesData());
return attributes.AsReadOnly();
}
}
internal class CustomAttributeProviderTestClass
{
public CustomAttributeProviderTestClass([MyParameter] int integerProperty)
{
IntegerProperty = integerProperty;
}
[MyProperty]
public int IntegerProperty { get; set; }
}
[Fact]
public void CustomAttributeProvider ()
{
var type = typeof(CustomAttributeProviderTestClass);
var propertyInfo = type.GetProperty(nameof(CustomAttributeProviderTestClass.IntegerProperty));
var ctorInfo = type.GetConstructor(new Type[] { typeof(int) });
var ctorParamInfo = ctorInfo.GetParameters()[0];
var propertyAndParamInfo = new PropertyAsParameterInfo(propertyInfo, ctorParamInfo);
// check GetCustomAttribute API
var cattrObjects = propertyAndParamInfo.GetCustomAttributes(true);
Assert.Equal(2, cattrObjects.Length);
Assert.Equal(typeof(MyParameterAttribute), cattrObjects[0].GetType());
Assert.Equal(typeof(MyPropertyAttribute), cattrObjects[1].GetType());
cattrObjects = propertyAndParamInfo.GetCustomAttributes(typeof(Attribute), true);
Assert.Equal(2, cattrObjects.Length);
Assert.Equal(typeof(MyParameterAttribute), cattrObjects[0].GetType());
Assert.Equal(typeof(MyPropertyAttribute), cattrObjects[1].GetType());
var cattrsEnumerable = propertyAndParamInfo.GetCustomAttributes();
Attribute[] cattrs = cattrsEnumerable.Cast<Attribute>().ToArray();
Assert.Equal(2, cattrs.Length);
Assert.Equal(typeof(MyParameterAttribute), cattrs[0].GetType());
Assert.Equal(typeof(MyPropertyAttribute), cattrs[1].GetType());
// check GetCustomAttributeData API
var customAttributesData = propertyAndParamInfo.GetCustomAttributesData();
Assert.Equal(2, customAttributesData.Count);
Assert.Equal(typeof(MyParameterAttribute), customAttributesData[0].AttributeType);
Assert.Equal(typeof(MyPropertyAttribute), customAttributesData[1].AttributeType);
}
}
}