/
UIHintAttribute.cs
207 lines (176 loc) · 8.97 KB
/
UIHintAttribute.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
// 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.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
namespace System.ComponentModel.DataAnnotations
{
/// <summary>
/// Attribute to provide a hint to the presentation layer about what control it should use
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
public class UIHintAttribute : Attribute
{
private readonly UIHintImplementation _implementation;
/// <summary>
/// Constructor that accepts the name of the control, without specifying which presentation layer to use
/// </summary>
/// <param name="uiHint">The name of the UI control.</param>
public UIHintAttribute(string uiHint)
: this(uiHint, null, Array.Empty<object>())
{
}
/// <summary>
/// Constructor that accepts both the name of the control as well as the presentation layer
/// </summary>
/// <param name="uiHint">The name of the control to use</param>
/// <param name="presentationLayer">The name of the presentation layer that supports this control</param>
public UIHintAttribute(string uiHint, string? presentationLayer)
: this(uiHint, presentationLayer, Array.Empty<object>())
{
}
/// <summary>
/// Full constructor that accepts the name of the control, presentation layer, and optional parameters
/// to use when constructing the control
/// </summary>
/// <param name="uiHint">The name of the control</param>
/// <param name="presentationLayer">The presentation layer</param>
/// <param name="controlParameters">The list of parameters for the control</param>
public UIHintAttribute(string uiHint, string? presentationLayer, params object?[]? controlParameters)
{
_implementation = new UIHintImplementation(uiHint, presentationLayer, controlParameters);
}
/// <summary>
/// Gets the name of the control that is most appropriate for this associated property or field
/// </summary>
public string UIHint => _implementation.UIHint;
/// <summary>
/// Gets the name of the presentation layer that supports the control type in <see cref="UIHint" />
/// </summary>
public string? PresentationLayer => _implementation.PresentationLayer;
/// <summary>
/// Gets the name-value pairs used as parameters to the control's constructor
/// </summary>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is ill-formed.</exception>
public IDictionary<string, object?> ControlParameters => _implementation.ControlParameters;
public override int GetHashCode() => _implementation.GetHashCode();
public override bool Equals([NotNullWhen(true)] object? obj) =>
obj is UIHintAttribute otherAttribute && _implementation.Equals(otherAttribute._implementation);
internal sealed class UIHintImplementation
{
private readonly object?[]? _inputControlParameters;
private IDictionary<string, object?>? _controlParameters;
public UIHintImplementation(string uiHint, string? presentationLayer, params object?[]? controlParameters)
{
UIHint = uiHint;
PresentationLayer = presentationLayer;
if (controlParameters != null)
{
_inputControlParameters = new object[controlParameters.Length];
Array.Copy(controlParameters, _inputControlParameters, controlParameters.Length);
}
}
/// <summary>
/// Gets the name of the control that is most appropriate for this associated property or field
/// </summary>
public string UIHint { get; }
/// <summary>
/// Gets the name of the presentation layer that supports the control type in <see cref="UIHint" />
/// </summary>
public string? PresentationLayer { get; }
// Lazy load the dictionary. It's fine if this method executes multiple times in stress scenarios.
// If the method throws (indicating that the input params are invalid) this property will throw
// every time it's accessed.
public IDictionary<string, object?> ControlParameters =>
_controlParameters ??= BuildControlParametersDictionary();
/// <summary>
/// Returns the hash code for this UIHintAttribute.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
var a = UIHint ?? string.Empty;
var b = PresentationLayer ?? string.Empty;
return a.GetHashCode() ^ b.GetHashCode();
}
/// <summary>
/// Determines whether this instance of UIHintAttribute and a specified object,
/// which must also be a UIHintAttribute object, have the same value.
/// </summary>
/// <param name="obj">An System.Object.</param>
/// <returns>true if obj is a UIHintAttribute and its value is the same as this instance; otherwise, false.</returns>
public override bool Equals([NotNullWhen(true)] object? obj)
{
var otherImplementation = obj as UIHintImplementation;
if (otherImplementation is null ||
UIHint != otherImplementation.UIHint ||
PresentationLayer != otherImplementation.PresentationLayer)
{
return false;
}
IDictionary<string, object?> leftParams;
IDictionary<string, object?> rightParams;
try
{
leftParams = ControlParameters;
rightParams = otherImplementation.ControlParameters;
}
catch (InvalidOperationException)
{
return false;
}
Debug.Assert(leftParams != null, "leftParams shouldn't be null");
Debug.Assert(rightParams != null, "rightParams shouldn't be null");
if (leftParams.Count != rightParams.Count)
{
return false;
}
return leftParams.OrderBy(p => p.Key).SequenceEqual(rightParams.OrderBy(p => p.Key));
}
/// <summary>
/// Validates the input control parameters and throws InvalidOperationException if they are not correct.
/// </summary>
/// <returns>
/// Dictionary of control parameters.
/// </returns>
private Dictionary<string, object?> BuildControlParametersDictionary()
{
var controlParameters = new Dictionary<string, object?>();
object?[]? inputControlParameters = _inputControlParameters;
if (inputControlParameters == null || inputControlParameters.Length == 0)
{
return controlParameters;
}
if (inputControlParameters.Length % 2 != 0)
{
throw new InvalidOperationException(SR.UIHintImplementation_NeedEvenNumberOfControlParameters);
}
for (int i = 0; i < inputControlParameters.Length; i += 2)
{
object? key = inputControlParameters[i];
object? value = inputControlParameters[i + 1];
if (key == null)
{
throw new InvalidOperationException(SR.Format(SR.UIHintImplementation_ControlParameterKeyIsNull, i));
}
if (!(key is string keyString))
{
throw new InvalidOperationException(SR.Format(SR.UIHintImplementation_ControlParameterKeyIsNotAString,
i,
inputControlParameters[i]!.ToString()));
}
if (controlParameters.ContainsKey(keyString))
{
throw new InvalidOperationException(SR.Format(SR.UIHintImplementation_ControlParameterKeyOccursMoreThanOnce,
i,
keyString));
}
controlParameters[keyString] = value;
}
return controlParameters;
}
}
}
}