使用代码模式+特性 完成了对于参数的效验。 使用时只需要在函数传入的参数前加入指定特性就可以进行效验, 可添加新的效验方法 ,写法比较优。
ArgumentValidationAttribute.cs
- namespace Validate
- {
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- /// <summary>
- /// 此类为验证标签的抽象父类
- /// 参数说明:
- /// object value 代表需要验证的数据
- /// </summary>
- public abstract class ArgumentValidationAttribute : Attribute
- {
- public abstract void Validate(object value);
- }
-
- /// <summary>
- /// 非空验证
- /// </summary>
- [AttributeUsage(AttributeTargets.Parameter)]
- public class NotNullAttribute : ArgumentValidationAttribute
- {
- public override void Validate(object value)
- {
- if (value == null || value.ToString().Trim().Length == 0)
- throw new ArgumentNullException("", "姓名不能为空");
- }
- }
-
- /// <summary>
- /// 范围验证
- /// </summary>
- [AttributeUsage(AttributeTargets.Parameter)]
- public class InRangeAttribute : ArgumentValidationAttribute
- {
- private int min;
- private int max;
-
- public InRangeAttribute(int min, int max)
- {
- this.min = min;
- this.max = max;
- }
-
- public override void Validate(object value)
- {
- int intValue = Convert.ToInt32(value);
- if (intValue < min || intValue > max)
- {
- throw new ArgumentOutOfRangeException("", string.Format("年龄须在{0}到{1}之间", min, max));
- }
- }
- }
- }
复制代码 DynamicProxy.cs
- namespace Validate
- {
- using System;
- using System.Dynamic;
- using System.Reflection;
- /// <summary>
- /// 此类用于代理模式
- /// 参数说明:
- /// Object target 代表代理的类型
- /// 自定义方法:
- /// Validate 开始验证
- /// </summary>
- public class DynamicProxy : DynamicObject
- {
- private Object target;
-
- public DynamicProxy(Object target)
- {
- this.target = target;
- }
- private void Validate(InvokeMemberBinder binder, object[] args)
- {
- Type type = target.GetType();
- MethodInfo info = type.GetMethod(binder.Name);
- ParameterInfo[] parameters = info.GetParameters();
- for (int i = 0; i < parameters.Length; i++)
- {
- var attributes = parameters[i].GetCustomAttributes(typeof(ArgumentValidationAttribute), false);
- foreach (ArgumentValidationAttribute f in attributes)
- {
- f.Validate(args[i]);
- }
- }
- }
- public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
- {
- try
- {
- this.Validate(binder, args);
- result = target.GetType().InvokeMember(binder.Name, BindingFlags.InvokeMethod, null, target, args);
- return true;
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- }
复制代码 FormMain.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using Validate;
-
- namespace 自定义特性验证_WindowsForm_
- {
- public partial class FormMain : Form
- {
- public FormMain()
- {
- InitializeComponent();
- }
-
- private void FormMain_Load(object sender, EventArgs e)
- {
-
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- string name = textBox1.Text;
- string age = textBox2.Text;
- try
- {
- ControlValidate v = new ControlValidate();
- dynamic proxy = new DynamicProxy(v);
- proxy.Reg(name , age);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
-
- public class ControlValidate
- {
- public void Reg([NotNull]string name , [InRange(10,20)]string age)
- {
- MessageBox.Show("验证通过");
- }
- }
- }
复制代码
|
|