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.ComponentModel;
33 namespace System.ComponentModel.DataAnnotations
35 [AttributeUsage (AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
36 public class RangeAttribute : ValidationAttribute
38 Func <object, bool> comparer;
41 public object Maximum { get; private set; }
42 public object Minimum { get; private set; }
43 public Type OperandType { get; private set; }
45 IComparable MaximumComparable {
46 get { return Maximum as IComparable; }
49 IComparable MinimumComparable {
50 get { return Minimum as IComparable; }
54 : base (GetDefaultErrorMessage)
58 public RangeAttribute (double minimum, double maximum) : this ()
62 OperandType = typeof (double);
65 public RangeAttribute (int minimum, int maximum) : this ()
69 OperandType = typeof (int);
72 public RangeAttribute (Type type, string minimum, string maximum) : this ()
76 throw new ArgumentNullException ("type");
82 comparer = SetupComparer ();
86 static string GetDefaultErrorMessage ()
88 return "The field {0} must be between {1} and {2}.";
91 public override string FormatErrorMessage (string name)
94 comparer = SetupComparer ();
96 return String.Format (ErrorMessageString, name, Minimum, Maximum);
99 // LAMESPEC: does not throw ValidationException when value is out of range
100 public override bool IsValid (object value)
102 if (comparer == null)
103 comparer = SetupComparer ();
108 string s = value as string;
109 if (s != null && s.Length == 0)
113 if (comparer != null)
114 return comparer (value);
117 } catch (FormatException) {
119 } catch (InvalidCastException) {
124 Func <object, bool> SetupComparer ()
126 Type ot = OperandType;
128 object min = Minimum, max = Maximum;
130 if (min == null || max == null)
131 throw new InvalidOperationException ("The minimum and maximum values must be set.");
134 return new Func <object, bool> (CompareInt);
137 return new Func <object, bool> (CompareDouble);
140 throw new InvalidOperationException ("The OperandType must be set when strings are used for minimum and maximum values.");
142 if (!typeof(IComparable).IsAssignableFrom (ot)) {
144 string message = String.Format ("The type {0} must implement System.IComparable", ot.FullName);
145 throw new InvalidOperationException (message);
147 throw new ArgumentException ("object");
151 string smin = min as string, smax = max as string;
152 cvt = TypeDescriptor.GetConverter (ot);
153 Minimum = cvt.ConvertFromString (smin);
154 Maximum = cvt.ConvertFromString (smax);
156 return new Func <object, bool> (CompareArbitrary);
159 bool CompareInt (object value)
161 int cv = Convert.ToInt32 (value);
163 return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
166 bool CompareDouble (object value)
168 double cv = Convert.ToDouble (value);
170 return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
173 bool CompareArbitrary (object value)
176 if (value != null && value.GetType () == OperandType)
178 else if (cvt != null)
179 cv = cvt.ConvertFrom (value);
183 return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;