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
36 [AttributeUsage (AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
38 [AttributeUsage (AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false)]
40 public class RangeAttribute : ValidationAttribute
42 Func <object, bool> comparer;
45 public object Maximum { get; private set; }
46 public object Minimum { get; private set; }
47 public Type OperandType { get; private set; }
49 IComparable MaximumComparable {
50 get { return Maximum as IComparable; }
53 IComparable MinimumComparable {
54 get { return Minimum as IComparable; }
58 : base (GetDefaultErrorMessage)
62 public RangeAttribute (double minimum, double maximum) : this ()
66 OperandType = typeof (double);
69 public RangeAttribute (int minimum, int maximum) : this ()
73 OperandType = typeof (int);
76 public RangeAttribute (Type type, string minimum, string maximum) : this ()
80 throw new ArgumentNullException ("type");
86 comparer = SetupComparer ();
90 static string GetDefaultErrorMessage ()
92 return "The field {0} must be between {1} and {2}.";
95 public override string FormatErrorMessage (string name)
98 comparer = SetupComparer ();
100 return String.Format (ErrorMessageString, name, Minimum, Maximum);
103 // LAMESPEC: does not throw ValidationException when value is out of range
104 public override bool IsValid (object value)
106 if (comparer == null)
107 comparer = SetupComparer ();
112 string s = value as string;
113 if (s != null && s.Length == 0)
117 if (comparer != null)
118 return comparer (value);
121 } catch (FormatException) {
123 } catch (InvalidCastException) {
128 Func <object, bool> SetupComparer ()
130 Type ot = OperandType;
132 object min = Minimum, max = Maximum;
134 if (min == null || max == null)
135 throw new InvalidOperationException ("The minimum and maximum values must be set.");
138 return new Func <object, bool> (CompareInt);
141 return new Func <object, bool> (CompareDouble);
144 throw new InvalidOperationException ("The OperandType must be set when strings are used for minimum and maximum values.");
146 if (!typeof(IComparable).IsAssignableFrom (ot)) {
148 string message = String.Format ("The type {0} must implement System.IComparable", ot.FullName);
149 throw new InvalidOperationException (message);
151 throw new ArgumentException ("object");
155 string smin = min as string, smax = max as string;
156 cvt = TypeDescriptor.GetConverter (ot);
157 Minimum = cvt.ConvertFromString (smin);
158 Maximum = cvt.ConvertFromString (smax);
160 return new Func <object, bool> (CompareArbitrary);
163 bool CompareInt (object value)
165 int cv = Convert.ToInt32 (value);
167 return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
170 bool CompareDouble (object value)
172 double cv = Convert.ToDouble (value);
174 return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;
177 bool CompareArbitrary (object value)
180 if (value != null && value.GetType () == OperandType)
182 else if (cvt != null)
183 cv = cvt.ConvertFrom (value);
187 return MinimumComparable.CompareTo (cv) <= 0 && MaximumComparable.CompareTo (cv) >= 0;