Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CustomValidationAttribute.cs
Go to the documentation of this file.
3using System.Linq;
5
7
8[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = true)]
10{
12
14
15 private string _lastMessage;
16
18
20
22
23 [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
24 public Type ValidatorType { get; }
25
26 public override object TypeId
27 {
28 get
29 {
30 if (_typeId == null)
31 {
33 }
34 return _typeId;
35 }
36 }
37
38 public string Method { get; }
39
40 public override bool RequiresValidationContext
41 {
42 get
43 {
46 }
47 }
48
49 public CustomValidationAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type validatorType, string method)
50 : base(() => System.SR.CustomValidationAttribute_ValidationError)
51 {
52 ValidatorType = validatorType;
53 Method = method;
55 }
56
57 protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
58 {
60 MethodInfo methodInfo = _methodInfo;
61 if (!TryConvertValue(value, out var convertedValue))
62 {
64 }
65 try
66 {
67 object[] parameters = ((!_isSingleArgumentMethod) ? new object[2] { convertedValue, validationContext } : new object[1] { convertedValue });
68 ValidationResult validationResult = (ValidationResult)methodInfo.Invoke(null, parameters);
69 _lastMessage = null;
70 if (validationResult != null)
71 {
72 _lastMessage = validationResult.ErrorMessage;
73 }
74 return validationResult;
75 }
77 {
78 throw ex.InnerException;
79 }
80 }
81
82 public override string FormatErrorMessage(string name)
83 {
85 if (!string.IsNullOrEmpty(_lastMessage))
86 {
87 return string.Format(CultureInfo.CurrentCulture, _lastMessage, name);
88 }
89 return base.FormatErrorMessage(name);
90 }
91
96
109
110 private string ValidateMethodParameter()
111 {
112 if (string.IsNullOrEmpty(Method))
113 {
115 }
116 MethodInfo methodInfo = ValidatorType.GetMethods(BindingFlags.Static | BindingFlags.Public).SingleOrDefault((MethodInfo m) => string.Equals(m.Name, Method, StringComparison.Ordinal));
117 if (methodInfo == null)
118 {
120 }
121 if (!typeof(ValidationResult).IsAssignableFrom(methodInfo.ReturnType))
122 {
124 }
125 ParameterInfo[] parameters = methodInfo.GetParameters();
126 if (parameters.Length == 0 || parameters[0].ParameterType.IsByRef)
127 {
129 }
130 _isSingleArgumentMethod = parameters.Length == 1;
131 if (!_isSingleArgumentMethod && (parameters.Length != 2 || parameters[1].ParameterType != typeof(ValidationContext)))
132 {
134 }
135 _methodInfo = methodInfo;
136 _firstParameterType = parameters[0].ParameterType;
137 return null;
138 }
139
141 {
143 if (value != null)
144 {
146 }
147 }
148
149 private bool TryConvertValue(object value, out object convertedValue)
150 {
151 convertedValue = null;
152 Type firstParameterType = _firstParameterType;
153 if (value == null)
154 {
155 if (firstParameterType.IsValueType && (!firstParameterType.IsGenericType || firstParameterType.GetGenericTypeDefinition() != typeof(Nullable<>)))
156 {
157 return false;
158 }
159 return true;
160 }
161 if (firstParameterType.IsInstanceOfType(value))
162 {
163 convertedValue = value;
164 return true;
165 }
166 try
167 {
168 convertedValue = Convert.ChangeType(value, firstParameterType, CultureInfo.CurrentCulture);
169 return true;
170 }
171 catch (FormatException)
172 {
173 return false;
174 }
176 {
177 return false;
178 }
180 {
181 return false;
182 }
183 }
184}
override bool Equals([NotNullWhen(true)] object? obj)
Definition Attribute.cs:691
CustomValidationAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type validatorType, string method)
override? ValidationResult IsValid(object? value, ValidationContext validationContext)
static ? object ChangeType(object? value, TypeCode typeCode)
Definition Convert.cs:229
Exception? InnerException
Definition Exception.cs:104
static CultureInfo CurrentCulture
T Value
Definition Lazy.cs:37
ParameterInfo[] GetParameters()
object? Invoke(object? obj, object?[]? parameters)
static string CustomValidationAttribute_Method_Required
Definition SR.cs:32
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string CustomValidationAttribute_Method_Must_Return_ValidationResult
Definition SR.cs:28
static string CustomValidationAttribute_Type_Conversion_Failed
Definition SR.cs:36
static string CustomValidationAttribute_Method_Not_Found
Definition SR.cs:30
static string CustomValidationAttribute_ValidatorType_Required
Definition SR.cs:42
static string CustomValidationAttribute_Method_Signature
Definition SR.cs:34
static string CustomValidationAttribute_Type_Must_Be_Public
Definition SR.cs:38
Definition SR.cs:7
bool IsValueType
Definition Type.cs:234
virtual bool IsInstanceOfType([NotNullWhen(true)] object? o)
Definition Type.cs:1020
virtual bool IsGenericType
Definition Type.cs:111
bool IsVisible
Definition Type.cs:364
MethodInfo[] GetMethods()
Definition Type.cs:788
bool IsByRef
Definition Type.cs:73
virtual Type GetGenericTypeDefinition()
Definition Type.cs:495