/
DisplayAttribute.cs
443 lines (411 loc) · 21.7 KB
/
DisplayAttribute.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
namespace System.ComponentModel.DataAnnotations
{
/// <summary>
/// DisplayAttribute is a general-purpose attribute to specify user-visible globalizable strings for types and members.
/// The string properties of this class can be used either as literals or as resource identifiers into a specified
/// <see cref="ResourceType" />
/// </summary>
[AttributeUsage(
AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Method | AttributeTargets.Class,
AllowMultiple = false)]
public sealed class DisplayAttribute : Attribute
{
#region Member Fields
private readonly LocalizableString _description = new LocalizableString(nameof(Description));
private readonly LocalizableString _groupName = new LocalizableString(nameof(GroupName));
private readonly LocalizableString _name = new LocalizableString(nameof(Name));
private readonly LocalizableString _prompt = new LocalizableString(nameof(Prompt));
private readonly LocalizableString _shortName = new LocalizableString(nameof(ShortName));
private bool? _autoGenerateField;
private bool? _autoGenerateFilter;
private int? _order;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
private Type? _resourceType;
#endregion
#region All Constructors
#endregion
#region Properties
/// <summary>
/// Gets or sets the ShortName attribute property, which may be a resource key string.
/// <para>
/// Consumers must use the <see cref="GetShortName" /> method to retrieve the UI display string.
/// </para>
/// </summary>
/// <remarks>
/// The property contains either the literal, non-localized string or the resource key
/// to be used in conjunction with <see cref="ResourceType" /> to configure a localized
/// short name for display.
/// <para>
/// The <see cref="GetShortName" /> method will return either the literal, non-localized
/// string or the localized string when <see cref="ResourceType" /> has been specified.
/// </para>
/// </remarks>
/// <value>
/// The short name is generally used as the grid column label for a UI element bound to the member
/// bearing this attribute. A <c>null</c> or empty string is legal, and consumers must allow for that.
/// </value>
public string? ShortName
{
get => _shortName.Value;
set => _shortName.Value = value;
}
/// <summary>
/// Gets or sets the Name attribute property, which may be a resource key string.
/// <para>
/// Consumers must use the <see cref="GetName" /> method to retrieve the UI display string.
/// </para>
/// </summary>
/// <remarks>
/// The property contains either the literal, non-localized string or the resource key
/// to be used in conjunction with <see cref="ResourceType" /> to configure a localized
/// name for display.
/// <para>
/// The <see cref="GetName" /> method will return either the literal, non-localized
/// string or the localized string when <see cref="ResourceType" /> has been specified.
/// </para>
/// </remarks>
/// <value>
/// The name is generally used as the field label for a UI element bound to the member
/// bearing this attribute. A <c>null</c> or empty string is legal, and consumers must allow for that.
/// </value>
public string? Name
{
get => _name.Value;
set => _name.Value = value;
}
/// <summary>
/// Gets or sets the Description attribute property, which may be a resource key string.
/// <para>
/// Consumers must use the <see cref="GetDescription" /> method to retrieve the UI display string.
/// </para>
/// </summary>
/// <remarks>
/// The property contains either the literal, non-localized string or the resource key
/// to be used in conjunction with <see cref="ResourceType" /> to configure a localized
/// description for display.
/// <para>
/// The <see cref="GetDescription" /> method will return either the literal, non-localized
/// string or the localized string when <see cref="ResourceType" /> has been specified.
/// </para>
/// </remarks>
/// <value>
/// Description is generally used as a tool tip or description a UI element bound to the member
/// bearing this attribute. A <c>null</c> or empty string is legal, and consumers must allow for that.
/// </value>
public string? Description
{
get => _description.Value;
set => _description.Value = value;
}
/// <summary>
/// Gets or sets the Prompt attribute property, which may be a resource key string.
/// <para>
/// Consumers must use the <see cref="GetPrompt" /> method to retrieve the UI display string.
/// </para>
/// </summary>
/// <remarks>
/// The property contains either the literal, non-localized string or the resource key
/// to be used in conjunction with <see cref="ResourceType" /> to configure a localized
/// prompt for display.
/// <para>
/// The <see cref="GetPrompt" /> method will return either the literal, non-localized
/// string or the localized string when <see cref="ResourceType" /> has been specified.
/// </para>
/// </remarks>
/// <value>
/// A prompt is generally used as a prompt or watermark for a UI element bound to the member
/// bearing this attribute. A <c>null</c> or empty string is legal, and consumers must allow for that.
/// </value>
public string? Prompt
{
get => _prompt.Value;
set => _prompt.Value = value;
}
/// <summary>
/// Gets or sets the GroupName attribute property, which may be a resource key string.
/// <para>
/// Consumers must use the <see cref="GetGroupName" /> method to retrieve the UI display string.
/// </para>
/// </summary>
/// <remarks>
/// The property contains either the literal, non-localized string or the resource key
/// to be used in conjunction with <see cref="ResourceType" /> to configure a localized
/// group name for display.
/// <para>
/// The <see cref="GetGroupName" /> method will return either the literal, non-localized
/// string or the localized string when <see cref="ResourceType" /> has been specified.
/// </para>
/// </remarks>
/// <value>
/// A group name is used for grouping fields into the UI. A <c>null</c> or empty string is legal,
/// and consumers must allow for that.
/// </value>
public string? GroupName
{
get => _groupName.Value;
set => _groupName.Value = value;
}
/// <summary>
/// Gets or sets the <see cref="System.Type" /> that contains the resources for <see cref="ShortName" />,
/// <see cref="Name" />, <see cref="Description" />, <see cref="Prompt" />, and <see cref="GroupName" />.
/// Using <see cref="ResourceType" /> along with these Key properties, allows the <see cref="GetShortName" />,
/// <see cref="GetName" />, <see cref="GetDescription" />, <see cref="GetPrompt" />, and <see cref="GetGroupName" />
/// methods to return localized values.
/// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
public Type? ResourceType
{
get => _resourceType;
set
{
if (_resourceType != value)
{
_resourceType = value;
_shortName.ResourceType = value;
_name.ResourceType = value;
_description.ResourceType = value;
_prompt.ResourceType = value;
_groupName.ResourceType = value;
}
}
}
/// <summary>
/// Gets or sets whether UI should be generated automatically to display this field. If this property is not
/// set then the presentation layer will automatically determine whether UI should be generated. Setting this
/// property allows an override of the default behavior of the presentation layer.
/// <para>
/// Consumers must use the <see cref="GetAutoGenerateField" /> method to retrieve the value, as this property
/// getter will throw an exception if the value has not been set.
/// </para>
/// </summary>
/// <exception cref="System.InvalidOperationException">
/// If the getter of this property is invoked when the value has not been explicitly set using the setter.
/// </exception>
public bool AutoGenerateField
{
get
{
if (!_autoGenerateField.HasValue)
{
throw new InvalidOperationException(SR.Format(SR.DisplayAttribute_PropertyNotSet,
nameof(AutoGenerateField),
nameof(GetAutoGenerateField)));
}
return _autoGenerateField.GetValueOrDefault();
}
set => _autoGenerateField = value;
}
/// <summary>
/// Gets or sets whether UI should be generated automatically to display filtering for this field. If this property is
/// not set then the presentation layer will automatically determine whether filtering UI should be generated. Setting this
/// property allows an override of the default behavior of the presentation layer.
/// <para>
/// Consumers must use the <see cref="GetAutoGenerateFilter" /> method to retrieve the value, as this property
/// getter will throw
/// an exception if the value has not been set.
/// </para>
/// </summary>
/// <exception cref="System.InvalidOperationException">
/// If the getter of this property is invoked when the value has not been explicitly set using the setter.
/// </exception>
public bool AutoGenerateFilter
{
get
{
if (!_autoGenerateFilter.HasValue)
{
throw new InvalidOperationException(SR.Format(SR.DisplayAttribute_PropertyNotSet,
nameof(AutoGenerateFilter),
nameof(GetAutoGenerateFilter)));
}
return _autoGenerateFilter.GetValueOrDefault();
}
set => _autoGenerateFilter = value;
}
/// <summary>
/// Gets or sets the order in which this field should be displayed. If this property is not set then
/// the presentation layer will automatically determine the order. Setting this property explicitly
/// allows an override of the default behavior of the presentation layer.
/// <para>
/// Consumers must use the <see cref="GetOrder" /> method to retrieve the value, as this property getter will throw
/// an exception if the value has not been set.
/// </para>
/// </summary>
/// <exception cref="System.InvalidOperationException">
/// If the getter of this property is invoked when the value has not been explicitly set using the setter.
/// </exception>
public int Order
{
get
{
if (!_order.HasValue)
{
throw new InvalidOperationException(SR.Format(SR.DisplayAttribute_PropertyNotSet,
nameof(Order),
nameof(GetOrder)));
}
return _order.GetValueOrDefault();
}
set => _order = value;
}
#endregion
#region Methods
/// <summary>
/// Gets the UI display string for ShortName.
/// <para>
/// This can be either a literal, non-localized string provided to <see cref="ShortName" /> or the
/// localized string found when <see cref="ResourceType" /> has been specified and <see cref="ShortName" />
/// represents a resource key within that resource type.
/// </para>
/// </summary>
/// <returns>
/// When <see cref="ResourceType" /> has not been specified, the value of
/// <see cref="ShortName" /> will be returned.
/// <para>
/// When <see cref="ResourceType" /> has been specified and <see cref="ShortName" />
/// represents a resource key within that resource type, then the localized value will be returned.
/// </para>
/// <para>
/// If <see cref="ShortName" /> is <c>null</c>, the value from <see cref="GetName" /> will be returned.
/// </para>
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// After setting both the <see cref="ResourceType" /> property and the <see cref="ShortName" /> property,
/// but a public static property with a name matching the <see cref="ShortName" /> value couldn't be found
/// on the <see cref="ResourceType" />.
/// </exception>
public string? GetShortName() => _shortName.GetLocalizableValue() ?? GetName();
/// <summary>
/// Gets the UI display string for Name.
/// <para>
/// This can be either a literal, non-localized string provided to <see cref="Name" /> or the
/// localized string found when <see cref="ResourceType" /> has been specified and <see cref="Name" />
/// represents a resource key within that resource type.
/// </para>
/// </summary>
/// <returns>
/// When <see cref="ResourceType" /> has not been specified, the value of
/// <see cref="Name" /> will be returned.
/// <para>
/// When <see cref="ResourceType" /> has been specified and <see cref="Name" />
/// represents a resource key within that resource type, then the localized value will be returned.
/// </para>
/// <para>
/// Can return <c>null</c> and will not fall back onto other values, as it's more likely for the
/// consumer to want to fall back onto the property name.
/// </para>
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// After setting both the <see cref="ResourceType" /> property and the <see cref="Name" /> property,
/// but a public static property with a name matching the <see cref="Name" /> value couldn't be found
/// on the <see cref="ResourceType" />.
/// </exception>
public string? GetName() => _name.GetLocalizableValue();
/// <summary>
/// Gets the UI display string for Description.
/// <para>
/// This can be either a literal, non-localized string provided to <see cref="Description" /> or the
/// localized string found when <see cref="ResourceType" /> has been specified and <see cref="Description" />
/// represents a resource key within that resource type.
/// </para>
/// </summary>
/// <returns>
/// When <see cref="ResourceType" /> has not been specified, the value of
/// <see cref="Description" /> will be returned.
/// <para>
/// When <see cref="ResourceType" /> has been specified and <see cref="Description" />
/// represents a resource key within that resource type, then the localized value will be returned.
/// </para>
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// After setting both the <see cref="ResourceType" /> property and the <see cref="Description" /> property,
/// but a public static property with a name matching the <see cref="Description" /> value couldn't be found
/// on the <see cref="ResourceType" />.
/// </exception>
public string? GetDescription() => _description.GetLocalizableValue();
/// <summary>
/// Gets the UI display string for Prompt.
/// <para>
/// This can be either a literal, non-localized string provided to <see cref="Prompt" /> or the
/// localized string found when <see cref="ResourceType" /> has been specified and <see cref="Prompt" />
/// represents a resource key within that resource type.
/// </para>
/// </summary>
/// <returns>
/// When <see cref="ResourceType" /> has not been specified, the value of
/// <see cref="Prompt" /> will be returned.
/// <para>
/// When <see cref="ResourceType" /> has been specified and <see cref="Prompt" />
/// represents a resource key within that resource type, then the localized value will be returned.
/// </para>
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// After setting both the <see cref="ResourceType" /> property and the <see cref="Prompt" /> property,
/// but a public static property with a name matching the <see cref="Prompt" /> value couldn't be found
/// on the <see cref="ResourceType" />.
/// </exception>
public string? GetPrompt() => _prompt.GetLocalizableValue();
/// <summary>
/// Gets the UI display string for GroupName.
/// <para>
/// This can be either a literal, non-localized string provided to <see cref="GroupName" /> or the
/// localized string found when <see cref="ResourceType" /> has been specified and <see cref="GroupName" />
/// represents a resource key within that resource type.
/// </para>
/// </summary>
/// <returns>
/// When <see cref="ResourceType" /> has not been specified, the value of
/// <see cref="GroupName" /> will be returned.
/// <para>
/// When <see cref="ResourceType" /> has been specified and <see cref="GroupName" />
/// represents a resource key within that resource type, then the localized value will be returned.
/// </para>
/// </returns>
/// <exception cref="System.InvalidOperationException">
/// After setting both the <see cref="ResourceType" /> property and the <see cref="GroupName" /> property,
/// but a public static property with a name matching the <see cref="GroupName" /> value couldn't be found
/// on the <see cref="ResourceType" />.
/// </exception>
public string? GetGroupName() => _groupName.GetLocalizableValue();
/// <summary>
/// Gets the value of <see cref="AutoGenerateField" /> if it has been set, or <c>null</c>.
/// </summary>
/// <returns>
/// When <see cref="AutoGenerateField" /> has been set returns the value of that property.
/// <para>
/// When <see cref="AutoGenerateField" /> has not been set returns <c>null</c>.
/// </para>
/// </returns>
public bool? GetAutoGenerateField() => _autoGenerateField;
/// <summary>
/// Gets the value of <see cref="AutoGenerateFilter" /> if it has been set, or <c>null</c>.
/// </summary>
/// <returns>
/// When <see cref="AutoGenerateFilter" /> has been set returns the value of that property.
/// <para>
/// When <see cref="AutoGenerateFilter" /> has not been set returns <c>null</c>.
/// </para>
/// </returns>
public bool? GetAutoGenerateFilter() => _autoGenerateFilter;
/// <summary>
/// Gets the value of <see cref="Order" /> if it has been set, or <c>null</c>.
/// </summary>
/// <returns>
/// When <see cref="Order" /> has been set returns the value of that property.
/// <para>
/// When <see cref="Order" /> has not been set returns <c>null</c>.
/// </para>
/// </returns>
/// <remarks>
/// When an order is not specified, presentation layers should consider using the value
/// of 10000. This value allows for explicitly-ordered fields to be displayed before
/// and after the fields that don't specify an order.
/// </remarks>
public int? GetOrder() => _order;
#endregion
}
}