All those tasty configuration validators new in .Net are wonderful. However they always have limitations that you cannot easily overcome due to the complexity of the validation required (i.e. Must match this value but only when this other flag is set). In this case we have the wonderful CallbackValidator class which actually just calls the supplied delegate. It is configured via the CallbackValidatorAttribute that you apply on your custom ConfigurationElement types. The issue comes up however that there's some serious limitations to the one build into .Net runtime.
- The callback must be public. Yuck.
- The callback must be static. Double yuck
- You cannot override this behavior as the attribute is sealed. Strike three!
Well not liking this I've taken the liberty to implement my own custom version that allows you to overcome all these limitations.
//Copyright (c) 2006 James Zimmerman
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to
//deal in the Software without restriction, including without limitation the
//rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
//sell copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
//IN THE SOFTWARE.
/// <summary>
/// Specifies a <see cref="CallbackValidator"/> object to use for code validation. The usage of this type is similar to the <see cref="CallbackValidatorAttribute"/> class.
/// </summary>
/// <remarks>
/// This validator attribute is a custom behaviorial implementation of the <see cref="CallbackValidatorAttribute"/> class.
/// This version allows public and private, static and instance methods to be used unlike the intrinsic version.
/// </remarks>
[AttributeUsage(AttributeTargets.Property)]
[Serializable()]
public class EnhancedCallbackValidatorAttribute : ConfigurationValidatorAttribute
{
#region Fields
[field:NonSerialized()]
private ValidatorCallback _callbackMethod;
private readonly String _callbackMethodName;
private readonly Type validatingType;
private readonly Type validatedType;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="EnhancedCallbackValidatorAttribute"/> class with the supplied values.
/// </summary>
/// <param name="callbackMethodName">The <see cref="CallbackMethodName"/> value.</param>
/// <param name="validatingType">The <see cref="ValidatingType"/> value.</param>
/// <param name="validatedType">The <see cref="ValidatedType"/> value.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="callbackMethodName"/> is <see langkeyword="null">null</see> or <see cref="string.Empty">empty</see></para>
/// <para>-OR-</para>
/// <para><paramref name="validatingType"/> is <see langkeyword="null">null</see></para>
/// <para>-OR-</para>
/// <para><paramref name="validatedType"/> is <see langkeyword="null">null</see></para>
/// </exception>
public EnhancedCallbackValidatorAttribute(String callbackMethodName, Type validatingType, Type validatedType)
{
callbackMethodName = (callbackMethodName ?? String.Empty).Trim();
if (String.IsNullOrEmpty(callbackMethodName)) throw new ArgumentNullException("callbackMethodName");
if (validatingType == null) throw new ArgumentNullException("validatingType");
if (validatedType == null) throw new ArgumentNullException("validatedType");
this._callbackMethodName = callbackMethodName;
this.validatingType = validatingType;
this.validatedType = validatedType;
}
#endregion
#region Properties
/// <summary>
/// Gets the name of the callback method to use on type <see cref="ValidatingType"/>.
/// </summary>
/// <remarks>
/// <para>The named method can be a static or instance method. The visibility can be non-public.
/// If the method is an instance method a new instance of the <see cref="ValidatingType"/> class will be created for each validation.
/// A created instance requires a parameterless constructor of any visibility to be declared.</para>
/// </remarks>
/// <value>The name of the callback method to use on type <see cref="ValidatingType"/>.</value>
public String CallbackMethodName
{
get
{
return this._callbackMethodName;
}
}
/// <summary>
/// Gets the type of the validator that the <see cref="CallbackMethodName"/> is declared on.
/// </summary>
public Type ValidatingType
{
get
{
return this.validatingType;
}
}
/// <summary>
/// Gets the <see cref="Type"/> of the object that will be validated.
/// </summary>
/// <remarks>Generally this is the type of the configuration property that the validator will act on.</remarks>
/// <value>The <see cref="Type"/> of the object that will be validated.</value>
public Type ValidatedType
{
get
{
return this.validatedType;
}
}
/// <summary>
/// Gets the validator instance.
/// </summary>
/// <value>The current <see cref="ConfigurationValidatorBase"/> instance.</value>
/// <exception cref="InvalidOperationException">
/// <para>The <see cref="CallbackMethodName"/> is an instance method and the <see cref="ValidatingType"/> does not have a parameterless constructor.</para>
/// <para>-OR-</para>
/// <para>The <see cref="CallbackMethodName"/> property is not set to a void method with one object parameter or the indicated method name was not found on type <see cref="ValidatingType"/>.</para>
/// </exception>
public override ConfigurationValidatorBase ValidatorInstance
{
get
{
if (this._callbackMethod != null) return new CallbackValidator(this.validatedType, this._callbackMethod);
MethodInfo method = this.FindMethod();
Object target = this.CreateMethodTarget(method);
this._callbackMethod = this.CreateDelegate(method, target);
return new CallbackValidator(this.validatedType, this._callbackMethod);
}
}
#endregion
#region Methods
/// <summary>
/// Finds the appropriate method for the <see cref="CallbackMethodName"/>.
/// </summary>
/// <returns>The found <see cref="MethodInfo"/> matching <see cref="CallbackMethodName"/>.</returns>
/// <exception cref="InvalidOperationException">If the appropriate method cannot be found then this exception should be thrown indicating the issue.</exception>
protected virtual MethodInfo FindMethod()
{
const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
MethodInfo method = this.validatingType.GetMethod(this._callbackMethodName, flags, null, new[] {typeof (Object)}, null);
if (method == null) throw new InvalidOperationException(String.Format("The supplied method name '{0}' was not found on type '{1}'.", this._callbackMethodName, this.validatingType.FullName));
return method;
}
/// <summary>
/// Creates the appropriate instance of <see cref="ValidatingType"/> if needed.
/// </summary>
/// <remarks>This method should return <see langkeyword="null">null</see> if the supplied <paramref name="method"/> does not require an instance of the type to operate on.</remarks>
/// <param name="method">The required <see cref="MethodInfo"/> to create an optional instance of the <see cref="Type.DeclaringType">declaring type</see>.</param>
/// <returns>If an instance is required, the instance; otherwise <see langkeyword="null">null</see>.</returns>
/// <exception cref="InvalidOperationException">If the appropriate instance type cannot be created then this expection should be thrown indicating the issue.</exception>
protected virtual Object CreateMethodTarget(MethodInfo method)
{
if (method.IsStatic) return null;
try
{
Object target = Activator.CreateInstance(this.ValidatingType, true);
return target;
}
catch (MissingMethodException ex)
{
throw new InvalidOperationException(String.Format("The method '{0}' is an instance method but the type '{1}' cannot be created as it does not have a parameterless constructor defined.", this.CallbackMethodName, this.ValidatingType.FullName), ex);
}
}
/// <summary>
/// Creates the appropriate <see cref="ValidatorCallback"/> to be used for validation.
/// </summary>
/// <param name="method">The required <see cref="MethodInfo"/> to create an optional instance of the <see cref="Type.DeclaringType">declaring type</see>.</param>
/// <param name="target">The optional method target the created delegate should leverage.</param>
/// <returns>The <see cref="ValidatorCallback"/> to invoke.</returns>
/// <exception cref="InvalidOperationException">If the appropriate delegate type cannot be created (such as due to a method parameter mismatch) then this expection should be thrown indicating the issue.</exception>
protected virtual ValidatorCallback CreateDelegate(MethodInfo method, Object target)
{
ParameterInfo[] parameters = method.GetParameters();
if ((parameters.Length == 1) && (parameters[0].ParameterType == typeof(Object)))
{
ValidatorCallback callback = this._callbackMethod = (ValidatorCallback)Delegate.CreateDelegate(typeof(ValidatorCallback), target, method);
return callback;
}
throw new ArgumentException(String.Format("The supplied method name '{0}' was not found. The callback method must be a void method with one object parameter.", this._callbackMethodName));
}
#endregion
}
enjoy! |