5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell Inc. http://novell.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
32 using System.ComponentModel;
34 namespace System.ComponentModel.DataAnnotations
37 [AttributeUsage (AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
39 [AttributeUsage (AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
41 public class RangeAttribute : ValidationAttribute
43 Func <object, bool> comparer;
46 public object Maximum { get; private set; }
47 public object Minimum { get; private set; }
48 public Type OperandType { get; private set; }
50 IComparable MaximumComparable {
51 get { return Maximum as IComparable; }
54 IComparable MinimumComparable {
55 get { return Minimum as IComparable; }
59 : base (GetDefaultErrorMessage)
63 public RangeAttribute (double minimum, double maximum) : this ()
67 OperandType = typeof (double);
70 public RangeAttribute (int minimum, int maximum) : this ()
74 OperandType = typeof (int);
77 public RangeAttribute (Type type, string minimum, string maximum) : this ()
81 throw new ArgumentNullException ("type");
87 comparer = SetupComparer ();
91 static string GetDefaultErrorMessage ()
93 return "The field {0} must be between {1} and {2}.";
96 public override string FormatErrorMessage (string name)
99 comparer = SetupComparer ();
101 return String.Format (ErrorMessageString, name, Minimum, Maximum);
104 // LAMESPEC: does not throw ValidationException when value is out of range
105 public override bool IsValid (object value)
107 if (comparer == null)
108 comparer = SetupComparer ();
113 string s = value as string;
114 if (s != null && s.Length == 0)
118 if (comparer != null)
119 return comparer (value);
122 } catch (FormatException) {
124 } catch (InvalidCastException) {
129 Func <object, bool> SetupComparer ()
131 Type ot = OperandType;
133 object min = Minimum, max = Maximum;
135 if (min == null || max == null)
136 throw new InvalidOperationException ("The minimum and maximum values must be set.");
139 return new Func <object, bool> (CompareInt);
142 return new Func <object, bool> (CompareDouble);
145 throw new InvalidOperationException ("The OperandType must be set when strings are used for minimum and maximum values.");
147 if (!typeof(IComparable).IsAssignableFrom (ot)) {
149 string message = String.Format ("The type {0} must implement System.IComparable", ot.FullName);
150 throw new InvalidOperationException (message);
152 throw new ArgumentException ("object");
156 string smin = min as string, smax = max as string;
157 cvt = TypeDescriptor.GetConverter (ot);
158 Minimum = cvt.ConvertFromString (smin);
159 Maximum = cvt.ConvertFromString (smax);
161 return new Func <object, bool> (CompareArbitrary);
164 bool CompareInt (object value)
166 int cv = Convert.ToInt32 (value);
168 return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
171 bool CompareDouble (object value)
173 double cv = Convert.ToDouble (value);
175 return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
178 bool CompareArbitrary (object value)
181 if (value != null && value.GetType () == OperandType)
183 else if (cvt != null)
184 cv = cvt.ConvertFrom (value);
188 return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;