This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ModelAttributes.cs
260 lines (231 loc) · 11.6 KB
/
ModelAttributes.cs
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
/// <summary>
/// Provides access to the combined list of attributes associated with a <see cref="Type"/>, property, or parameter.
/// </summary>
public class ModelAttributes
{
private static readonly IEnumerable<object> _emptyAttributesCollection = Enumerable.Empty<object>();
/// <summary>
/// Creates a new <see cref="ModelAttributes"/> for a <see cref="Type"/>.
/// </summary>
/// <param name="typeAttributes">The set of attributes for the <see cref="Type"/>.</param>
[Obsolete("This constructor is obsolete and will be removed in a future version. The recommended alternative is " + nameof(ModelAttributes) + "." + nameof(GetAttributesForType) + ".")]
public ModelAttributes(IEnumerable<object> typeAttributes)
: this(typeAttributes, null, null)
{
}
/// <summary>
/// Creates a new <see cref="ModelAttributes"/> for a property.
/// </summary>
/// <param name="propertyAttributes">The set of attributes for the property.</param>
/// <param name="typeAttributes">
/// The set of attributes for the property's <see cref="Type"/>. See <see cref="PropertyInfo.PropertyType"/>.
/// </param>
[Obsolete("This constructor is obsolete and will be removed in a future version. The recommended alternative is " + nameof(ModelAttributes) + "." + nameof(GetAttributesForProperty) + ".")]
public ModelAttributes(IEnumerable<object> propertyAttributes, IEnumerable<object> typeAttributes)
: this(typeAttributes, propertyAttributes, null)
{
}
/// <summary>
/// Creates a new <see cref="ModelAttributes"/>.
/// </summary>
/// <param name="typeAttributes">
/// If this instance represents a type, the set of attributes for that type.
/// If this instance represents a property, the set of attributes for the property's <see cref="Type"/>.
/// Otherwise, <c>null</c>.
/// </param>
/// <param name="propertyAttributes">
/// If this instance represents a property, the set of attributes for that property.
/// Otherwise, <c>null</c>.
/// </param>
/// <param name="parameterAttributes">
/// If this instance represents a parameter, the set of attributes for that parameter.
/// Otherwise, <c>null</c>.
/// </param>
internal ModelAttributes(
IEnumerable<object> typeAttributes,
IEnumerable<object> propertyAttributes,
IEnumerable<object> parameterAttributes)
{
if (propertyAttributes != null)
{
// Represents a property
if (typeAttributes == null)
{
throw new ArgumentNullException(nameof(typeAttributes));
}
PropertyAttributes = propertyAttributes.ToArray();
TypeAttributes = typeAttributes.ToArray();
Attributes = PropertyAttributes.Concat(TypeAttributes).ToArray();
}
else if (parameterAttributes != null)
{
// Represents a parameter
if (typeAttributes == null)
{
throw new ArgumentNullException(nameof(typeAttributes));
}
ParameterAttributes = parameterAttributes.ToArray();
TypeAttributes = typeAttributes.ToArray();
Attributes = ParameterAttributes.Concat(TypeAttributes).ToArray();
}
else if (typeAttributes != null)
{
// Represents a type
if (typeAttributes == null)
{
throw new ArgumentNullException(nameof(typeAttributes));
}
Attributes = TypeAttributes = typeAttributes.ToArray();
}
}
/// <summary>
/// Gets the set of all attributes. If this instance represents the attributes for a property, the attributes
/// on the property definition are before those on the property's <see cref="Type"/>. If this instance
/// represents the attributes for a parameter, the attributes on the parameter definition are before those on
/// the parameter's <see cref="Type"/>.
/// </summary>
public IReadOnlyList<object> Attributes { get; }
/// <summary>
/// Gets the set of attributes on the property, or <c>null</c> if this instance does not represent the attributes
/// for a property.
/// </summary>
public IReadOnlyList<object> PropertyAttributes { get; }
/// <summary>
/// Gets the set of attributes on the parameter, or <c>null</c> if this instance does not represent the attributes
/// for a parameter.
/// </summary>
public IReadOnlyList<object> ParameterAttributes { get; }
/// <summary>
/// Gets the set of attributes on the <see cref="Type"/>. If this instance represents a property, then
/// <see cref="TypeAttributes"/> contains attributes retrieved from <see cref="PropertyInfo.PropertyType"/>.
/// If this instance represents a parameter, then contains attributes retrieved from
/// <see cref="ParameterInfo.ParameterType"/>.
/// </summary>
public IReadOnlyList<object> TypeAttributes { get; }
/// <summary>
/// Gets the attributes for the given <paramref name="property"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> in which caller found <paramref name="property"/>.
/// </param>
/// <param name="property">A <see cref="PropertyInfo"/> for which attributes need to be resolved.
/// </param>
/// <returns>
/// A <see cref="ModelAttributes"/> instance with the attributes of the property and its <see cref="Type"/>.
/// </returns>
public static ModelAttributes GetAttributesForProperty(Type type, PropertyInfo property)
{
return GetAttributesForProperty(type, property, property.PropertyType);
}
/// <summary>
/// Gets the attributes for the given <paramref name="property"/> with the specified <paramref name="modelType"/>.
/// </summary>
/// <param name="containerType">The <see cref="Type"/> in which caller found <paramref name="property"/>.
/// </param>
/// <param name="property">A <see cref="PropertyInfo"/> for which attributes need to be resolved.
/// </param>
/// <param name="modelType">The model type</param>
/// <returns>
/// A <see cref="ModelAttributes"/> instance with the attributes of the property and its <see cref="Type"/>.
/// </returns>
public static ModelAttributes GetAttributesForProperty(Type containerType, PropertyInfo property, Type modelType)
{
if (containerType == null)
{
throw new ArgumentNullException(nameof(containerType));
}
if (property == null)
{
throw new ArgumentNullException(nameof(property));
}
var propertyAttributes = property.GetCustomAttributes();
var typeAttributes = modelType.GetCustomAttributes();
var metadataType = GetMetadataType(containerType);
if (metadataType != null)
{
var metadataProperty = metadataType.GetRuntimeProperty(property.Name);
if (metadataProperty != null)
{
propertyAttributes = propertyAttributes.Concat(metadataProperty.GetCustomAttributes());
}
}
return new ModelAttributes(typeAttributes, propertyAttributes, parameterAttributes: null);
}
/// <summary>
/// Gets the attributes for the given <paramref name="type"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> for which attributes need to be resolved.
/// </param>
/// <returns>A <see cref="ModelAttributes"/> instance with the attributes of the <see cref="Type"/>.</returns>
public static ModelAttributes GetAttributesForType(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
var attributes = type.GetCustomAttributes();
var metadataType = GetMetadataType(type);
if (metadataType != null)
{
attributes = attributes.Concat(metadataType.GetCustomAttributes());
}
return new ModelAttributes(attributes, propertyAttributes: null, parameterAttributes: null);
}
/// <summary>
/// Gets the attributes for the given <paramref name="parameterInfo"/>.
/// </summary>
/// <param name="parameterInfo">
/// The <see cref="ParameterInfo"/> for which attributes need to be resolved.
/// </param>
/// <returns>
/// A <see cref="ModelAttributes"/> instance with the attributes of the parameter and its <see cref="Type"/>.
/// </returns>
public static ModelAttributes GetAttributesForParameter(ParameterInfo parameterInfo)
{
// Prior versions called IModelMetadataProvider.GetMetadataForType(...) and therefore
// GetAttributesForType(...) for parameters. Maintain that set of attributes (including those from an
// ModelMetadataTypeAttribute reference) for back-compatibility.
var typeAttributes = GetAttributesForType(parameterInfo.ParameterType).TypeAttributes;
var parameterAttributes = parameterInfo.GetCustomAttributes();
return new ModelAttributes(typeAttributes, propertyAttributes: null, parameterAttributes);
}
/// <summary>
/// Gets the attributes for the given <paramref name="parameterInfo"/> with the specified <paramref name="modelType"/>.
/// </summary>
/// <param name="parameterInfo">
/// The <see cref="ParameterInfo"/> for which attributes need to be resolved.
/// </param>
/// <param name="modelType">The model type.</param>
/// <returns>
/// A <see cref="ModelAttributes"/> instance with the attributes of the parameter and its <see cref="Type"/>.
/// </returns>
public static ModelAttributes GetAttributesForParameter(ParameterInfo parameterInfo, Type modelType)
{
if (parameterInfo == null)
{
throw new ArgumentNullException(nameof(parameterInfo));
}
if (modelType == null)
{
throw new ArgumentNullException(nameof(modelType));
}
// Prior versions called IModelMetadataProvider.GetMetadataForType(...) and therefore
// GetAttributesForType(...) for parameters. Maintain that set of attributes (including those from an
// ModelMetadataTypeAttribute reference) for back-compatibility.
var typeAttributes = GetAttributesForType(modelType).TypeAttributes;
var parameterAttributes = parameterInfo.GetCustomAttributes();
return new ModelAttributes(typeAttributes, propertyAttributes: null, parameterAttributes);
}
private static Type GetMetadataType(Type type)
{
return type.GetCustomAttribute<ModelMetadataTypeAttribute>()?.MetadataType;
}
}
}