CallbackValidatorAttribute
using System.Reflection;
namespace System.Configuration
{
[AttributeUsage(AttributeTargets.Property)]
public sealed class CallbackValidatorAttribute : ConfigurationValidatorAttribute
{
private ValidatorCallback _callbackMethod;
private string _callbackMethodName = string.Empty;
private Type _type;
public override ConfigurationValidatorBase ValidatorInstance {
get {
if (_callbackMethod == null) {
if (_type == (Type)null)
throw new ArgumentNullException("Type");
if (!string.IsNullOrEmpty(_callbackMethodName)) {
MethodInfo method = _type.GetMethod(_callbackMethodName, BindingFlags.Static | BindingFlags.Public);
if (method != (MethodInfo)null) {
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length == 1 && parameters[0].ParameterType == typeof(object))
_callbackMethod = (ValidatorCallback)Delegate.CreateDelegate(typeof(ValidatorCallback), method);
}
}
}
if (_callbackMethod == null)
throw new ArgumentException(System.SR.Format(System.SR.Validator_method_not_found, _callbackMethodName));
return new CallbackValidator(_callbackMethod);
}
}
public Type Type {
get {
return _type;
}
set {
_type = value;
_callbackMethod = null;
}
}
public string CallbackMethodName {
get {
return _callbackMethodName;
}
set {
_callbackMethodName = value;
_callbackMethod = null;
}
}
}
}