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
|
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Resources;
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>
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments", Justification = "ControlParameters is exposed, just with a different type")]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "We want users to be able to extend this class")]
public class UIHintAttribute : Attribute {
private UIHintImplementation _implementation;
/// <summary>
/// Gets the name of the control that is most appropriate for this associated property or field
/// </summary>
public string UIHint {
get {
return this._implementation.UIHint;
}
}
/// <summary>
/// Gets the name of the presentation layer that supports the control type in <see cref="UIHint"/>
/// </summary>
public string PresentationLayer {
get {
return this._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 {
get {
return this._implementation.ControlParameters;
}
}
#if !SILVERLIGHT
/// <summary>
/// Gets a unique identifier for this attribute.
/// </summary>
public override object TypeId {
get {
return this;
}
}
#endif
/// <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, new object[0]) {
}
/// <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, new object[0]) {
}
/// <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) {
this._implementation = new UIHintImplementation(uiHint, presentationLayer, controlParameters);
}
public override int GetHashCode() {
return this._implementation.GetHashCode();
}
public override bool Equals(object obj) {
var otherAttribute = obj as UIHintAttribute;
if (otherAttribute == null) {
return false;
}
return this._implementation.Equals(otherAttribute._implementation);
}
internal class UIHintImplementation {
private IDictionary<string, object> _controlParameters;
private object[] _inputControlParameters;
/// <summary>
/// Gets the name of the control that is most appropriate for this associated property or field
/// </summary>
public string UIHint { get; private set; }
/// <summary>
/// Gets the name of the presentation layer that supports the control type in <see cref="UIHint"/>
/// </summary>
public string PresentationLayer { get; private set; }
public IDictionary<string, object> ControlParameters {
get {
if (this._controlParameters == null) {
// 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.
this._controlParameters = this.BuildControlParametersDictionary();
}
return this._controlParameters;
}
}
public UIHintImplementation(string uiHint, string presentationLayer, params object[] controlParameters) {
this.UIHint = uiHint;
this.PresentationLayer = presentationLayer;
if (controlParameters != null) {
this._inputControlParameters = new object[controlParameters.Length];
Array.Copy(controlParameters, this._inputControlParameters, controlParameters.Length);
}
}
/// <summary>
/// Returns the hash code for this UIHintAttribute.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode() {
var a = this.UIHint ?? String.Empty;
var b = this.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(object obj) {
// don't need to perform a type check on obj since this is an internal class
var otherImplementation = (UIHintImplementation)obj;
if (this.UIHint != otherImplementation.UIHint || this.PresentationLayer != otherImplementation.PresentationLayer) {
return false;
}
IDictionary<string, object> leftParams;
IDictionary<string, object> rightParams;
try {
leftParams = this.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;
} else {
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 IDictionary<string, object> BuildControlParametersDictionary() {
IDictionary<string, object> controlParameters = new Dictionary<string, object>();
object[] inputControlParameters = this._inputControlParameters;
if (inputControlParameters == null || inputControlParameters.Length == 0) {
return controlParameters;
}
if (inputControlParameters.Length % 2 != 0) {
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, DataAnnotationsResources.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(
String.Format(
CultureInfo.CurrentCulture,
DataAnnotationsResources.UIHintImplementation_ControlParameterKeyIsNull,
i));
}
string keyString = key as string;
if (keyString == null) {
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
DataAnnotationsResources.UIHintImplementation_ControlParameterKeyIsNotAString,
i,
inputControlParameters[i].ToString()));
}
if (controlParameters.ContainsKey(keyString)) {
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
DataAnnotationsResources.UIHintImplementation_ControlParameterKeyOccursMoreThanOnce,
i,
keyString));
}
controlParameters[keyString] = value;
}
return controlParameters;
}
}
}
}
|