[练习]利用Attribute对实体进行验证

本文介绍了一种使用 C# 属性(Attribute)进行实体验证的方法,并提供了从 C# 4.0 到 C# 2.0 的实现案例。通过自定义属性和委托,实现了对实体类中不同类型的字段进行有效性检查。

早上看到了利用特性(Attribute)对实体类进行验证 这篇文章,碰上刚好最近对Attribute有点兴趣,所以对作者写的利用特性(Attribute)对实体类进行验证这篇文章进行练习并且对知识点进行巩固。

自己改写的版本如下:C#4.0

 

ExpandedBlockStart.gif C#4.0代码
namespace  ValidateDemo
{
    [Flags]
    
public   enum  ValidateType
    {
        NotEmpty
= 1 ,        
        MaxLength
= 2 ,
        MinLength
= 4 ,
        IsPhone 
=   8
        
    }
    
    [AttributeUsage(AttributeTargets.All,AllowMultiple
= true )]
    
public   class  ValidateAttribute : Attribute
    {
        
public  ValidateAttribute(ValidateType validateType)
        {
            ValidateType 
=  validateType;
        }
        
public  ValidateType ValidateType {  get set ; }
        
public   int  MinLength {  get set ; }
        
public   int  MaxLength {  get set ; }
        
public   string [] CustomArray {  get set ; }
    }
    
public   class  ValidateModel
    {
        
///   <summary>
        
///  验证类型
        
///   </summary>
         public  ValidateType Type{ get ; set ;}
        
///   <summary>
        
///  验证函数
        
///   </summary>
         public  Func < bool >  CheckFunc {  get set ; }
        
///   <summary>
        
///  错误信息
        
///   </summary>
         public   string  ErrorMessage {  get set ; }
    }
    
public   class  Validate
    {
        
        
///   <summary>
        
///  检查需要验证的函数是否通过
        
///   </summary>
        
///   <param name="checkType"> 被检查类型 </param>
        
///   <param name="matchType"> 需要检查的类型 </param>
        
///   <param name="func"> 检查函数 </param>
        
///   <param name="errMessage"> 错误信息 </param>
        
///   <returns> Emtpy 验证通过,否则返回错误信息 </returns>
         private   static   string  CheckValidate(ValidateType checkType,ValidateType matchType,Func < bool >  func,  string  errMessage)
        {
            
if  (checkType.HasFlag(matchType))
            {
                
if  (func())
                {
                    
return  errMessage;
                }               
            }
            
return  String.Empty;
        }

        
///   <summary>
        
///  检查对象是否通过验证
        
///   </summary>
        
///   <param name="entityObject"> 需要检查的对象 </param>
        
///   <param name="errMessage"> 返回错误信息 </param>
        
///   <returns> true:通过,false:失败 </returns>
         public   static   bool  GetValidateResult( object  entityObject,  out   string  errMessage)
        {
            Type type 
=  entityObject.GetType();
            PropertyInfo[] properties 
=  type.GetProperties();

            
string  validateResult  =   string .Empty;
            errMessage 
=   string .Empty;
            
foreach  (PropertyInfo property  in  properties)
            {
                
object [] validateContent  =  property.GetCustomAttributes( typeof (ValidateAttribute),  true );
                
if  (validateContent  !=   null )
                {
                    
object  value  =  property.GetValue(entityObject,  null );
                    
foreach  (ValidateAttribute validateAttribute  in  validateContent)
                    {
                        IList
< ValidateModel >  condition  =   new  List < ValidateModel > ();
                        
// 需要什么验证,在这里添加
                        condition.Add( new  ValidateModel { Type  =  ValidateType.NotEmpty, CheckFunc  =  ()  =>  {  return  (value  ==   null   ||  value.ToString().Length  <   1 ); }, ErrorMessage  =  String.Format( " 元素{0}不能为空。 " , property.Name) });
                        condition.Add(
new  ValidateModel { Type  =  ValidateType.MaxLength, CheckFunc  =  ()  =>  {  return  (value.ToString().Length  >  validateAttribute.MaxLength); }, ErrorMessage  =  String.Format( " 元素{0}长度不能超过{1} " , property.Name, validateAttribute.MaxLength) });
                        condition.Add(
new  ValidateModel { Type  =  ValidateType.MinLength, CheckFunc  =  ()  =>  {  return  (value.ToString().Length  <  validateAttribute.MinLength); }, ErrorMessage  =  String.Format( " 元素{0}长度不能小于{1} " , property.Name, validateAttribute.MinLength) });
                        condition.Add(
new  ValidateModel { Type  =  ValidateType.IsPhone, CheckFunc  =  ()  =>  {  return  ( ! System.Text.RegularExpressions.Regex.IsMatch(value.ToString(),  @" ^\d+$ " )); }, ErrorMessage  =  String.Format( " 元素{0}必须是电话号码 " , property.Name) });

                        
foreach  (ValidateModel model  in  condition)
                        {
                            validateResult 
=  CheckValidate(
                                    validateAttribute.ValidateType,
                                    model.Type,
                                    model.CheckFunc,
                                    model.ErrorMessage
                            );
                            
if  ( ! string .IsNullOrEmpty(validateResult))
                            {
                                errMessage 
=  validateResult;
                                
return   false ;
                            }
                        }
                    }
                }
            }
            
return   true ;
        }
    }

    
public   class  User
    {
        [Validate(ValidateType.MinLength,MinLength
= 5 )]
        [Validate(ValidateType.IsPhone)]
        
public   string  Password {  get set ; }
    }
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            
string  msg  =   string .Empty;
            User user 
=   new  User();
            user.Password 
=   " 31234a " ;
            
if  (Validate.GetValidateResult(user,  out  msg))
            {
                Console.WriteLine(
" 测试通过 " );
            }
            
else
            {
                Console.WriteLine(msg);
            }
            
        }

        
    }
}


 

但是基于公司的项目是C#2.0的,所以改成了2.0:

 testValidate.aspx

ExpandedBlockStart.gif 代码
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
    
< title ></ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
        Password:
< asp:TextBox ID = " TextBox1 "  runat = " server " ></ asp:TextBox >
        
< asp:Button ID = " Button1 "
            runat
= " server "  Text = " Button "  onclick = " Button1_Click "   />
    
</ div >
    
</ form >
</ body >
</ html >


 testValidate.aspx.cs

ExpandedBlockStart.gif C#2.0代码
using  System;
using  System.Collections.Generic;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Reflection;

public   partial   class  Darren_testValidate : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
    }
    
protected   void  Button1_Click( object  sender, EventArgs e)
    {
        
string  msg  =   string .Empty;
        User user 
=   new  User();
        user.Password 
=  TextBox1.Text.Trim();

        
if  (ValidateHelper.GetValidateResult(user,  out  msg))
        {
            Response.Write(
" 测试通过 " );
            
// Console.WriteLine("测试通过");
        }
        
else
        {
            Response.Write(msg);
            
// Console.WriteLine(msg);
        }
    }
}
[Flags]
public   enum  ValidateType
{
    NotEmpty 
=   1 ,
    MaxLength 
=   2 ,
    MinLength 
=   4 ,
    IsPhone 
=   8

}

[AttributeUsage(AttributeTargets.All, AllowMultiple 
=   true )]
public   class  ValidateAttribute : Attribute
{
    
public  ValidateAttribute(ValidateType validateType)
    {
        ValidateType 
=  validateType;
    }
    
private  ValidateType validateType;
    
private   int  minLength;
    
private   int  maxLength;    
    
public  ValidateType ValidateType {  get  { return  validateType;}  set {validateType = value;} }
    
public   int  MinLength {  get { return  minLength;}  set {minLength = value;} }
    
public   int  MaxLength {  get  {  return  maxLength; }  set  { maxLength  =  value; } }
}
public   class  ValidateModel
{
    
private  ValidateType type;
    
private  ValidateHelper.Func checkFunc;
    
private   string  errorMessage;
    
// private delegate bool func();
     ///   <summary>
    
///  验证类型
    
///   </summary>
     public  ValidateType Type {  get  {  return  type; }  set  { type  =  value; } }
    
///   <summary>
    
///  验证函数
    
///   </summary>
     public  ValidateHelper.Func CheckFunc {  get  {  return  checkFunc; }  set  { checkFunc  =  value; } }
    
///   <summary>
    
///  错误信息
    
///   </summary>
     public   string  ErrorMessage {  get  {  return  errorMessage; }  set  { errorMessage  =  value; } }
}
public   class  ValidateHelper
{
    
public   delegate   bool  Func();
    
///   <summary>
    
///  检查需要验证的函数是否通过
    
///   </summary>
    
///   <param name="checkType"> 被检查类型 </param>
    
///   <param name="matchType"> 需要检查的类型 </param>
    
///   <param name="func"> 检查函数 </param>
    
///   <param name="errMessage"> 错误信息 </param>
    
///   <returns> Emtpy 验证通过,否则返回错误信息 </returns>
     private   static   string  CheckValidate(ValidateType checkType, ValidateType matchType, Func func,  string  errMessage)
    {        
        
if  ((checkType  &  matchType)  !=   0 )
        {
            
if  (func())
            {
                
return  errMessage;
            }
        }
        
return  String.Empty;
    }

    
///   <summary>
    
///  检查对象是否通过验证
    
///   </summary>
    
///   <param name="entityObject"> 需要检查的对象 </param>
    
///   <param name="errMessage"> 返回错误信息 </param>
    
///   <returns> true:通过,false:失败 </returns>
     public   static   bool  GetValidateResult( object  entityObject,  out   string  errMessage)
    {
        Type type 
=  entityObject.GetType();
        PropertyInfo[] properties 
=  type.GetProperties();

        
string  validateResult  =   string .Empty;
        errMessage 
=   string .Empty;
        
foreach  (PropertyInfo property  in  properties)
        {
            
object [] validateContent  =  property.GetCustomAttributes( typeof (ValidateAttribute),  true );
            
if  (validateContent  !=   null )
            {
                
object  value  =  property.GetValue(entityObject,  null );
                
foreach  (ValidateAttribute validateAttribute  in  validateContent)
                {
                    IList
< ValidateModel >  condition  =   new  List < ValidateModel > ();
                    
// 需要什么验证,在这里添加
                    ValidateModel vmode  =   new  ValidateModel();
                    vmode.Type 
=  ValidateType.NotEmpty;
                    vmode.CheckFunc 
=   delegate  {  return  (value  ==   null   ||  value.ToString().Length  <   1 ); };
                    vmode.ErrorMessage 
=  String.Format( " 元素{0}不能为空。 " , property.Name);
                    condition.Add(vmode);

                    vmode 
=   new  ValidateModel();
                    vmode.Type 
=  ValidateType.MaxLength;
                    vmode.CheckFunc 
=   delegate  {  return  (value.ToString().Length  >  validateAttribute.MaxLength); };
                    vmode.ErrorMessage 
=  String.Format( " 元素{0}长度不能超过{1} " , property.Name, validateAttribute.MaxLength);
                    condition.Add(vmode);

                    vmode 
=   new  ValidateModel();
                    vmode.Type 
=  ValidateType.MinLength;
                    vmode.CheckFunc 
=   delegate  {  return  (value.ToString().Length  <  validateAttribute.MinLength); };
                    vmode.ErrorMessage 
=  String.Format( " 元素{0}长度不能小于{1} " , property.Name, validateAttribute.MinLength);
                    condition.Add(vmode);

                    vmode 
=   new  ValidateModel();
                    vmode.Type 
=  ValidateType.IsPhone;
                    vmode.CheckFunc 
=   delegate  {  return  ( ! System.Text.RegularExpressions.Regex.IsMatch(value.ToString(),  @" ^\d+$ " )); };
                    vmode.ErrorMessage 
=   String.Format( " 元素{0}必须是电话号码 " , property.Name);
                    condition.Add(vmode);
                    
// condition.Add(new ValidateModel { Type = ValidateType.NotEmpty, CheckFunc = () => { return (value == null || value.ToString().Length < 1); }, ErrorMessage = String.Format("元素{0}不能为空。", property.Name) });
                    
// condition.Add(new ValidateModel { Type = ValidateType.NotEmpty, CheckFunc = () => { return (value == null || value.ToString().Length < 1); }, ErrorMessage = String.Format("元素{0}不能为空。", property.Name) });
                    
// condition.Add(new ValidateModel { Type = ValidateType.MaxLength, CheckFunc = () => { return (value.ToString().Length > validateAttribute.MaxLength); }, ErrorMessage = String.Format("元素{0}长度不能超过{1}", property.Name, validateAttribute.MaxLength) });
                    
// condition.Add(new ValidateModel { Type = ValidateType.MinLength, CheckFunc = () => { return (value.ToString().Length < validateAttribute.MinLength); }, ErrorMessage = String.Format("元素{0}长度不能小于{1}", property.Name, validateAttribute.MinLength) });
                    
// condition.Add(new ValidateModel { Type = ValidateType.IsPhone, CheckFunc = () => { return (!System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), @"^\d+$")); }, ErrorMessage = String.Format("元素{0}必须是电话号码", property.Name) });

                    
foreach  (ValidateModel model  in  condition)
                    {
                        validateResult 
=  CheckValidate(
                                validateAttribute.ValidateType,
                                model.Type,
                                model.CheckFunc,
                                model.ErrorMessage
                        );
                        
if  ( ! string .IsNullOrEmpty(validateResult))
                        {
                            errMessage 
=  validateResult;
                            
return   false ;
                        }
                    }
                }
            }
        }
        
return   true ;
    }
}

public   class  User
{
    
private   string  password;
    [Validate(ValidateType.IsPhone 
|  ValidateType.MinLength  |  ValidateType.NotEmpty, MinLength  =   5 )]
    
public   string  Password {  get  {  return  password; }  set  { password  =  value; } }
}


 

 有博友提到“System.ComponentModel.DataAnnotations命名空间中有很多用来验证的Attribute。”也能进行验证,但是自己是作为练习,多写一些代码也无妨。

转载于:https://www.cnblogs.com/wbkt2t/archive/2010/08/24/1807175.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值