forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumHelper.cs
More file actions
293 lines (262 loc) · 13.6 KB
/
Copy pathEnumHelper.cs
File metadata and controls
293 lines (262 loc) · 13.6 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc.Properties;
namespace System.Web.Mvc.Html
{
public static class EnumHelper
{
/// <summary>
/// Gets a value indicating whether the given <paramref name="type"/> or an expression of this
/// <see cref="Type"/> is suitable for use in <see cref="GetSelectList(Type)"/> and <see
/// cref="SelectExtensions.EnumDropDownListFor{TModel,TEnum}(HtmlHelper{TModel}, Expression{Func{TModel, TEnum}})"/>
/// calls.
/// </summary>
/// <param name="type">The <see cref="Type"/> to check.</param>
/// <returns>
/// <see langword="true"/> if <see cref="GetSelectList(Type)"/> will not throw when passed given
/// <see cref="Type"/> and <see
/// cref="SelectExtensions.EnumDropDownListFor{TModel,TEnum}(HtmlHelper{TModel}, Expression{Func{TModel, TEnum}})"/>
/// will not throw when passed an expression of this <see cref="Type"/>; <see langword="false"/> otherwise.
/// </returns>
/// <remarks>
/// Currently returns <see langref="true"/> if the <paramref name="type"/> parameter is
/// non-<see langref="null"/>, is an <see langref="enum"/> type, and does not have a
/// <see cref="FlagsAttribute"/> attribute.
/// </remarks>
public static bool IsValidForEnumHelper(Type type)
{
bool isValid = false;
if (type != null)
{
// Type.IsEnum is false for Nullable<T> even if T is an enum. Check underlying type (if any).
// Do not support Enum type itself -- IsEnum property is false for that class.
Type checkedType = Nullable.GetUnderlyingType(type) ?? type;
if (checkedType.IsEnum)
{
isValid = !HasFlagsInternal(checkedType);
}
}
return isValid;
}
/// <summary>
/// Gets a value indicating whether the given <paramref name="metadata"/> or associated expression is suitable
/// for use in <see cref="GetSelectList(ModelMetadata)"/> and <see
/// cref="SelectExtensions.EnumDropDownListFor{TModel,TEnum}(HtmlHelper{TModel}, Expression{Func{TModel, TEnum}})"/>
/// calls.
/// </summary>
/// <param name="metadata">The <see cref="ModelMetadata"/> to check.</param>
/// <returns>
/// <see langword="true"/> if <see cref="GetSelectList(ModelMetadata)"/> will return not throw when passed
/// given <see cref="ModelMetadata"/> and <see
/// cref="SelectExtensions.EnumDropDownListFor{TModel,TEnum}(HtmlHelper{TModel}, Expression{Func{TModel, TEnum}})"/>
/// will not throw when passed associated expression; <see langword="false"/> otherwise.
/// </returns>
/// <remarks>
/// Currently returns <see langref="true"/> if the <paramref name="metadata"/> parameter is
/// non-<see langref="null"/> and <see cref="IsValidForEnumHelper(Type)"/> returns <see langref="true"/> for
/// <c>metadata.ModelType</c>.
/// </remarks>
public static bool IsValidForEnumHelper(ModelMetadata metadata)
{
return metadata != null && IsValidForEnumHelper(metadata.ModelType);
}
/// <summary>
/// Gets a list of <see cref="SelectListItem"/> objects corresponding to enum constants defined in the given
/// <paramref name="type"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> to evaluate.</param>
/// <returns> An <see cref="IList{SelectListItem}"/> for the given <paramref name="type"/>.</returns>
/// <remarks>
/// Throws if <see cref="IsValidForEnumHelper(Type)"/> returns <see langref="false"/> for the given
/// <paramref name="type"/>.
/// </remarks>
public static IList<SelectListItem> GetSelectList(Type type)
{
if (type == null)
{
throw Error.ArgumentNull("type");
}
if (!IsValidForEnumHelper(type))
{
throw Error.Argument("type", MvcResources.EnumHelper_InvalidParameterType, type.FullName);
}
IList<SelectListItem> selectList = new List<SelectListItem>();
// According to HTML5: "The first child option element of a select element with a required attribute and
// without a multiple attribute, and whose size is "1", must have either an empty value attribute, or must
// have no text content." SelectExtensions.DropDownList[For]() methods often generate a matching
// <select/>. Empty value for Nullable<T>, empty text for round-tripping an unrecognized value, or option
// label serves in some cases. But otherwise, ignoring this does not cause problems in either IE or Chrome.
Type checkedType = Nullable.GetUnderlyingType(type) ?? type;
if (checkedType != type)
{
// Underlying type was non-null so handle Nullable<T>; ensure returned list has a spot for null
selectList.Add(new SelectListItem { Text = String.Empty, Value = String.Empty, });
}
// Populate the list
const BindingFlags BindingFlags =
BindingFlags.DeclaredOnly | BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static;
foreach (FieldInfo field in checkedType.GetFields(BindingFlags))
{
// fieldValue will be an numeric type (byte, ...)
object fieldValue = field.GetRawConstantValue();
selectList.Add(new SelectListItem { Text = GetDisplayName(field), Value = fieldValue.ToString(), });
}
return selectList;
}
/// <summary>
/// Gets a list of <see cref="SelectListItem"/> objects corresponding to enum constants defined in the given
/// <paramref name="metadata"/>.
/// </summary>
/// <param name="metadata">The <see cref="ModelMetadata"/> to evaluate.</param>
/// <returns> An <see cref="IList{SelectListItem}"/> for the given <paramref name="metadata"/>.</returns>
/// <remarks>
/// Throws if <see cref="IsValidForEnumHelper(ModelMetadata)"/> returns <see langref="false"/> for the given
/// <paramref name="metadata"/>.
/// </remarks>
public static IList<SelectListItem> GetSelectList(ModelMetadata metadata)
{
if (metadata == null)
{
throw Error.ArgumentNull("metadata");
}
if (metadata.ModelType == null)
{
throw Error.Argument("metadata", MvcResources.EnumHelper_InvalidMetadataParameter);
}
if (!IsValidForEnumHelper(metadata))
{
throw Error.Argument("metadata", MvcResources.EnumHelper_InvalidParameterType,
metadata.ModelType.FullName);
}
return GetSelectList(metadata.ModelType);
}
/// <summary>
/// Gets a list of <see cref="SelectListItem"/> objects corresponding to enum constants defined in the given
/// <paramref name="type"/>. Also ensures the <paramref name="value"/> will round-trip even if it does not
/// match a defined constant and sets the <c>Selected</c> property to <see langref="true"/> for one element in
/// the returned list -- matching the <paramref name="value"/>.
/// </summary>
/// <param name="type">The <see cref="Type"/> to evaluate.</param>
/// <param name="value">Value from <see cref="Type"/> <paramref name="type"/> to select.</param>
/// <returns>
/// An <see cref="IList{SelectListItem}"/> for the given <paramref name="type"/>, possibly extended to
/// include an unrecognized <paramref name="value"/>.
/// </returns>
/// <remarks>
/// Throws if <see cref="IsValidForEnumHelper(Type)"/> returns <see langref="false"/> for the given
/// <paramref name="type"/> or if a non-null <paramref name="value"/> has a different <see cref="Type"/> than
/// <paramref name="type"/>.
/// </remarks>
public static IList<SelectListItem> GetSelectList(Type type, Enum value)
{
IList<SelectListItem> selectList = GetSelectList(type);
Type valueType = (value == null) ? null : value.GetType();
if (valueType != null && valueType != type && valueType != Nullable.GetUnderlyingType(type))
{
throw Error.Argument("value", MvcResources.EnumHelper_InvalidValueParameter, valueType.FullName,
type.FullName);
}
if (value == null && selectList.Count != 0 && String.IsNullOrEmpty(selectList[0].Value))
{
// Type is Nullable<T>; use existing entry to round trip null value
selectList[0].Selected = true;
}
else
{
// If null, use default for this (non-Nullable<T>) enum -- always has 0 integral value
string valueString = (value == null) ? "0" : value.ToString("d");
// Select the last matching item, imitating what at least IE and Chrome highlight when multiple
// elements in a <select/> element have a selected attribute.
bool foundSelected = false;
for (int i = selectList.Count - 1; !foundSelected && i >= 0; --i)
{
SelectListItem item = selectList[i];
item.Selected = (valueString == item.Value);
foundSelected |= item.Selected;
}
// Round trip the current value
if (!foundSelected)
{
if (selectList.Count != 0 && String.IsNullOrEmpty(selectList[0].Value))
{
// Type is Nullable<T>; use existing entry for round trip
selectList[0].Selected = true;
selectList[0].Value = valueString;
}
else
{
// Add new entry which does not display value to user
selectList.Insert(0,
new SelectListItem { Selected = true, Text = String.Empty, Value = valueString, });
}
}
}
return selectList;
}
/// <summary>
/// Gets a list of <see cref="SelectListItem"/> objects corresponding to enum constants defined in the given
/// <paramref name="metadata"/>. Also ensures the <paramref name="value"/> will round-trip even if it does not
/// match a defined constant and sets the <c>Selected</c> property to <see langref="true"/> for one element in
/// the returned list -- matching the <paramref name="value"/>.
/// </summary>
/// <param name="metadata">The <see cref="ModelMetadata"/> to evaluate.</param>
/// <param name="value">Value from <see cref="Type"/> of <paramref name="metadata"/> to select.</param>
/// <returns>
/// An <see cref="IList{SelectListItem}"/> for the given <paramref name="metadata"/>, possibly extended to
/// include an unrecognized <paramref name="value"/>.
/// </returns>
/// <remarks>
/// Throws if <see cref="IsValidForEnumHelper(ModelMetadata)"/> returns <see langref="false"/> for the given
/// <paramref name="metadata"/> or if a non-null <paramref name="value"/> has a different <see cref="Type"/>
/// than <c>metadata.ModelType</c>.
/// </remarks>
public static IList<SelectListItem> GetSelectList(ModelMetadata metadata, Enum value)
{
if (metadata == null)
{
throw Error.ArgumentNull("metadata");
}
if (metadata.ModelType == null)
{
throw Error.Argument("metadata", MvcResources.EnumHelper_InvalidMetadataParameter);
}
if (!IsValidForEnumHelper(metadata))
{
throw Error.Argument("metadata", MvcResources.EnumHelper_InvalidParameterType,
metadata.ModelType.FullName);
}
return GetSelectList(metadata.ModelType, value);
}
internal static bool HasFlags(Type type)
{
Contract.Assert(type != null);
Type checkedType = Nullable.GetUnderlyingType(type) ?? type;
return HasFlagsInternal(checkedType);
}
private static bool HasFlagsInternal(Type type)
{
Contract.Assert(type != null);
FlagsAttribute attribute = type.GetCustomAttribute<FlagsAttribute>(inherit: false);
return attribute != null;
}
// Return non-empty name specified in a [Display] attribute for the given field, if any; field's name otherwise
private static string GetDisplayName(FieldInfo field)
{
DisplayAttribute display = field.GetCustomAttribute<DisplayAttribute>(inherit: false);
if (display != null)
{
string name = display.GetName();
if (!String.IsNullOrEmpty(name))
{
return name;
}
}
return field.Name;
}
}
}