zoukankan      html  css  js  c++  java
  • 使用Expression 对成员Validation

    using System;
    using System.Collections.Generic;
    using System.Linq.Expressions;
    using System.Reflection;
    
    namespace ConsoleApplication1
    {
       internal class Program
       {
          private static void Main()
          {
    
             var student = new Student { Age = 15 };
             var p = new Parent { Role = "", Sex = "Male" };
             student.TheParent = p;
             student.Validate(s => s.Age, s => s.Name);
    
             student.Validate();
             Console.Read();
          }
       }
    
    
       public class Parent
       {
          public string Role { get; set; }
          public string Sex { get; set; }
       }
    
       public class Student
       {
          public Student()
          {
             BuildValidationMethods();
          }
    
          public int Age { get; set; }
          public string Name { get; set; }
          public Parent TheParent { get; set; }
    
          public void Validate(params Expression<Func<Student, object>>[] expression)
          {
             foreach (var expr in expression)
             {
                string proName = string.Empty;
                var unaryExpresson = expr.Body as UnaryExpression;
                if (unaryExpresson != null)
                {
                   proName = (unaryExpresson.Operand as MemberExpression).Member.Name;
                }
                else
                {
                   proName = ((MemberExpression)expr.Body).Member.Name;
                }
    
                Validate(proName);
             }
    
          }
          private readonly IDictionary<object, Action> _validateMethods = new Dictionary<object, Action>();
          public void Validate(string proName)
          {
             _validateMethods[proName]();
          }
    
          private string ExpressionToString<TProperty>(Expression<Func<Student, TProperty>> expression)
          {
             return ((MemberExpression)expression.Body).Member.Name;
          }
    
          public void BuildValidationMethods()
          {
             _validateMethods.Add(ExpressionToString(s=>s.Age), () => Console.WriteLine(Age < 18 ? "teen-agers" : "adult"));
             _validateMethods.Add(ExpressionToString(s=>Name), () =>
                {
                   if (String.IsNullOrEmpty(Name))
                      Console.WriteLine("Name is empty");
                });
             _validateMethods.Add(ExpressionToString(s=>s.TheParent.Role), () =>
                {
                   if (string.IsNullOrEmpty(TheParent.Role))
                   {
                      Console.WriteLine("Role is empty");
                   }
                });
          }
    
    
          public void Validate()
          {
             foreach (var validateMethod in _validateMethods.Values)
             {
                validateMethod();
             }
          }
       }
    }
  • 相关阅读:
    ASP.NET MVC布局
    C#微信扫码支付Demo
    ASP.NET MVC用户登录(Memcache存储用户登录信息)
    Memcached分布式缓存快速入门
    Log4Net日志配置
    ASP.NET MVC自定义异常处理
    Spring.Net快速入门:控制翻转、依赖注入、面向切面编程
    C#微信公众号开发入门教程
    APS.NET MVC4生成解析二维码简单Demo
    Entity Framwork学习笔记
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/2888889.html
Copyright © 2011-2022 走看看