A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

       使用代码模式+特性 完成了对于参数的效验。 使用时只需要在函数传入的参数前加入指定特性就可以进行效验, 可添加新的效验方法 ,写法比较优。
ArgumentValidationAttribute.cs
  1. namespace Validate
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Text;

  6.     /// <summary>
  7.     /// 此类为验证标签的抽象父类
  8.     /// 参数说明:
  9.     /// object value  代表需要验证的数据
  10.     /// </summary>
  11.     public abstract class ArgumentValidationAttribute : Attribute
  12.     {
  13.         public abstract void Validate(object value);
  14.     }

  15.     /// <summary>
  16.     /// 非空验证
  17.     /// </summary>
  18.     [AttributeUsage(AttributeTargets.Parameter)]
  19.     public class NotNullAttribute : ArgumentValidationAttribute
  20.     {
  21.         public override void Validate(object value)
  22.         {
  23.             if (value == null || value.ToString().Trim().Length == 0)
  24.                 throw new ArgumentNullException("", "姓名不能为空");
  25.         }
  26.     }

  27.     /// <summary>
  28.     /// 范围验证
  29.     /// </summary>
  30.     [AttributeUsage(AttributeTargets.Parameter)]
  31.     public class InRangeAttribute : ArgumentValidationAttribute
  32.     {
  33.         private int min;
  34.         private int max;

  35.         public InRangeAttribute(int min, int max)
  36.         {
  37.             this.min = min;
  38.             this.max = max;
  39.         }

  40.         public override void Validate(object value)
  41.         {
  42.             int intValue = Convert.ToInt32(value);
  43.             if (intValue < min || intValue > max)
  44.             {
  45.                 throw new ArgumentOutOfRangeException("", string.Format("年龄须在{0}到{1}之间", min, max));
  46.             }
  47.         }
  48.     }
  49. }
复制代码
DynamicProxy.cs
  1. namespace Validate
  2. {
  3.     using System;
  4.     using System.Dynamic;
  5.     using System.Reflection;
  6.     /// <summary>
  7.     /// 此类用于代理模式
  8.     /// 参数说明:
  9.     /// Object target         代表代理的类型
  10.     /// 自定义方法:
  11.     /// Validate                  开始验证
  12.     /// </summary>
  13.     public class DynamicProxy : DynamicObject
  14.     {
  15.         private Object target;

  16.         public DynamicProxy(Object target)
  17.         {
  18.             this.target = target;
  19.         }
  20.         private void Validate(InvokeMemberBinder binder, object[] args)
  21.         {
  22.             Type type = target.GetType();
  23.             MethodInfo info = type.GetMethod(binder.Name);
  24.             ParameterInfo[] parameters = info.GetParameters();
  25.             for (int i = 0; i < parameters.Length; i++)
  26.             {
  27.                 var attributes = parameters[i].GetCustomAttributes(typeof(ArgumentValidationAttribute), false);
  28.                 foreach (ArgumentValidationAttribute f in attributes)
  29.                 {
  30.                     f.Validate(args[i]);
  31.                 }
  32.             }
  33.         }
  34.         public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
  35.         {
  36.             try
  37.             {
  38.                 this.Validate(binder, args);
  39.                 result = target.GetType().InvokeMember(binder.Name, BindingFlags.InvokeMethod, null, target, args);
  40.                 return true;
  41.             }
  42.             catch (Exception)
  43.             {
  44.                 throw;
  45.             }
  46.         }
  47.     }
  48. }
复制代码
FormMain.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Validate;

  10. namespace 自定义特性验证_WindowsForm_
  11. {
  12.     public partial class FormMain : Form
  13.     {
  14.         public FormMain()
  15.         {
  16.             InitializeComponent();
  17.         }

  18.         private void FormMain_Load(object sender, EventArgs e)
  19.         {

  20.         }

  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             string name = textBox1.Text;
  24.             string age = textBox2.Text;
  25.             try
  26.             {
  27.                 ControlValidate v = new ControlValidate();
  28.                 dynamic proxy  = new DynamicProxy(v);
  29.                 proxy.Reg(name , age);
  30.             }
  31.             catch (Exception ex)
  32.             {
  33.                 MessageBox.Show(ex.Message);
  34.             }
  35.         }
  36.     }

  37.     public class ControlValidate
  38.     {
  39.         public void Reg([NotNull]string name , [InRange(10,20)]string age)
  40.         {
  41.             MessageBox.Show("验证通过");
  42.         }
  43.     }
  44. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马