comparison Source/Validation/MaxValueAttribute.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2
3 namespace BLToolkit.Validation
4 {
5 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
6 public class MaxValueAttribute : ValidatorBaseAttribute
7 {
8 public MaxValueAttribute(object maxValue)
9 : this(maxValue, false)
10 {
11 }
12
13 public MaxValueAttribute(object maxValue, string errorMessage)
14 : this(maxValue, false, errorMessage)
15 {
16 }
17
18 public MaxValueAttribute(object maxValue, bool isExclusive)
19 {
20 _value = maxValue;
21 _isExclusive = isExclusive;
22 }
23
24 public MaxValueAttribute(object maxValue, bool isExclusive, string errorMessage)
25 : this(maxValue, isExclusive)
26 {
27 ErrorMessage = errorMessage;
28 }
29
30 private readonly object _value;
31 public virtual object GetValue(ValidationContext context)
32 {
33 return _value;
34 }
35
36 private bool _isExclusive;
37 public bool IsExclusive
38 {
39 get { return _isExclusive; }
40 set { _isExclusive = value; }
41 }
42
43 public override bool IsValid(ValidationContext context)
44 {
45 if (context.IsNull(context))
46 return true;
47
48 object contextValue = context.Value;
49 object testValue = GetValue(context);
50
51 if (contextValue is Int32)
52 {
53 Int32 tv = Convert.ToInt32(testValue);
54 return tv > (Int32)contextValue || !IsExclusive && tv == (Int32)contextValue;
55 }
56
57 if (contextValue is decimal)
58 {
59 decimal tv = Convert.ToDecimal(testValue);
60 return tv > (decimal)contextValue || !IsExclusive && tv == (decimal)contextValue;
61 }
62
63 if (contextValue is double)
64 {
65 double tv = Convert.ToDouble(testValue);
66 return tv > (double)contextValue || !IsExclusive && tv == (double)contextValue;
67 }
68
69 if (contextValue is float)
70 {
71 float tv = Convert.ToSingle(testValue);
72 return tv > (float)contextValue || !IsExclusive && tv == (float)contextValue;
73 }
74
75 if (contextValue is byte)
76 {
77 byte tv = Convert.ToByte(testValue);
78 return tv > (byte)contextValue || !IsExclusive && tv == (byte)contextValue;
79 }
80
81 if (contextValue is char)
82 {
83 char tv = Convert.ToChar(testValue);
84 return tv > (char)contextValue || !IsExclusive && tv == (char)contextValue;
85 }
86
87 if (contextValue is Int16)
88 {
89 Int16 tv = Convert.ToInt16(testValue);
90 return tv > (Int16)contextValue || !IsExclusive && tv == (Int16)contextValue;
91 }
92
93 if (contextValue is sbyte)
94 {
95 sbyte tv = Convert.ToSByte(testValue);
96 return tv > (sbyte)contextValue || !IsExclusive && tv == (sbyte)contextValue;
97 }
98
99 if (contextValue is UInt16)
100 {
101 UInt16 tv = Convert.ToUInt16(testValue);
102 return tv > (UInt16)contextValue || !IsExclusive && tv == (UInt16)contextValue;
103 }
104
105 if (contextValue is UInt32)
106 {
107 UInt32 tv = Convert.ToUInt32(testValue);
108 return tv > (UInt32)contextValue || !IsExclusive && tv == (UInt32)contextValue;
109 }
110
111 if (contextValue is Int64)
112 {
113 Int64 tv = Convert.ToInt64(testValue);
114 return tv > (Int64)contextValue || !IsExclusive && tv == (Int64)contextValue;
115 }
116
117 if (contextValue is UInt64)
118 {
119 UInt64 tv = Convert.ToUInt64(testValue);
120 return tv > (UInt64)contextValue || !IsExclusive && tv == (UInt64)contextValue;
121 }
122
123 return true;
124 }
125
126 public override string ErrorMessage
127 {
128 get { return base.ErrorMessage ?? "Maximum value for '{0}' is {1}{2}."; }
129 set { base.ErrorMessage = value; }
130 }
131
132 public override string GetErrorMessage(ValidationContext context)
133 {
134 return string.Format(ErrorMessage,
135 GetPropertyFriendlyName(context),
136 GetValue(context),
137 IsExclusive? " exclusive": string.Empty);
138 }
139 }
140 }