Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ValidationAttribute.cs
Go to the documentation of this file.
4
6
7public abstract class ValidationAttribute : Attribute
8{
9 private string _errorMessage;
10
11 private Func<string> _errorMessageResourceAccessor;
12
14
15 [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
17
18 private volatile bool _hasBaseIsValid;
19
20 private string _defaultErrorMessage;
21
22 internal string? DefaultErrorMessage
23 {
24 set
25 {
29 }
30 }
31
32 protected string ErrorMessageString
33 {
34 get
35 {
38 }
39 }
40
41 internal bool CustomErrorMessageSet { get; private set; }
42
43 public virtual bool RequiresValidationContext => false;
44
45 public string? ErrorMessage
46 {
47 get
48 {
50 }
51 set
52 {
56 if (value == null)
57 {
59 }
60 }
61 }
62
64 {
65 get
66 {
68 }
69 set
70 {
74 }
75 }
76
77 [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
79 {
80 get
81 {
83 }
84 set
85 {
89 }
90 }
91
93 : this(() => System.SR.ValidationAttribute_ValidationError)
94 {
95 }
96
97 protected ValidationAttribute(string errorMessage)
98 {
99 string errorMessage2 = errorMessage;
100 this._002Ector(() => errorMessage2);
101 }
102
103 protected ValidationAttribute(Func<string> errorMessageAccessor)
104 {
105 _errorMessageResourceAccessor = errorMessageAccessor;
106 }
107
109 {
111 {
112 return;
113 }
114 string localErrorMessage = ErrorMessage;
115 bool flag = !string.IsNullOrEmpty(_errorMessageResourceName);
116 bool flag2 = !string.IsNullOrEmpty(_errorMessage);
117 bool flag3 = _errorMessageResourceType != null;
118 bool flag4 = !string.IsNullOrEmpty(_defaultErrorMessage);
119 if ((flag && flag2) || !(flag || flag2 || flag4))
120 {
122 }
123 if (flag3 != flag)
124 {
126 }
127 if (flag)
128 {
130 return;
131 }
132 _errorMessageResourceAccessor = () => localErrorMessage;
133 }
134
136 {
138 if (property != null)
139 {
140 MethodInfo getMethod = property.GetMethod;
141 if (getMethod == null || (!getMethod.IsAssembly && !getMethod.IsPublic))
142 {
143 property = null;
144 }
145 }
146 if (property == null)
147 {
149 }
150 if (property.PropertyType != typeof(string))
151 {
153 }
154 _errorMessageResourceAccessor = () => (string)property.GetValue(null, null);
155 }
156
157 public virtual string FormatErrorMessage(string name)
158 {
159 return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name);
160 }
161
162 public virtual bool IsValid(object? value)
163 {
164 if (!_hasBaseIsValid)
165 {
166 _hasBaseIsValid = true;
167 }
168 return IsValid(value, null) == ValidationResult.Success;
169 }
170
171 protected virtual ValidationResult? IsValid(object? value, ValidationContext validationContext)
172 {
173 if (_hasBaseIsValid)
174 {
176 }
178 if (!IsValid(value))
179 {
180 string memberName = validationContext.MemberName;
181 string[] memberNames = ((memberName == null) ? null : new string[1] { memberName });
182 result = new ValidationResult(FormatErrorMessage(validationContext.DisplayName), memberNames);
183 }
184 return result;
185 }
186
188 {
189 if (validationContext == null)
190 {
191 throw new ArgumentNullException("validationContext");
192 }
193 ValidationResult validationResult = IsValid(value, validationContext);
194 if (validationResult != null && string.IsNullOrEmpty(validationResult.ErrorMessage))
195 {
196 string errorMessage = FormatErrorMessage(validationContext.DisplayName);
197 validationResult = new ValidationResult(errorMessage, validationResult.MemberNames);
198 }
199 return validationResult;
200 }
201
202 public void Validate(object? value, string name)
203 {
204 if (!IsValid(value))
205 {
206 throw new ValidationException(FormatErrorMessage(name), this, value);
207 }
208 }
209
210 public void Validate(object? value, ValidationContext validationContext)
211 {
212 if (validationContext == null)
213 {
214 throw new ArgumentNullException("validationContext");
215 }
216 ValidationResult validationResult = GetValidationResult(value, validationContext);
217 if (validationResult != null)
218 {
219 throw new ValidationException(validationResult, this, value);
220 }
221 }
222}
ValidationResult? GetValidationResult(object? value, ValidationContext validationContext)
virtual ? ValidationResult IsValid(object? value, ValidationContext validationContext)
void Validate(object? value, ValidationContext validationContext)
static CultureInfo CurrentCulture
static Exception ByDesignWithMessage(string message)
static string ValidationAttribute_Cannot_Set_ErrorMessage_And_Resource
Definition SR.cs:104
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ValidationAttribute_ResourceTypeDoesNotHaveProperty
Definition SR.cs:112
static string ValidationAttribute_NeedBothResourceTypeAndResourceName
Definition SR.cs:108
static string ValidationAttribute_IsValid_NotImplemented
Definition SR.cs:106
static string ValidationAttribute_ResourcePropertyNotStringType
Definition SR.cs:110
Definition SR.cs:7
PropertyInfo? GetProperty(string name)
Definition Type.cs:815
string? FullName
Definition Type.cs:47