自定义元数据(Attribute)来标识对象(属性,方法,类)

代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Reflection;
using  System.Web.UI.WebControls;
using  System.ComponentModel;
using  System.Web.UI;
using  System.Windows.Forms;

namespace  CustomControl
{
    
public   class  CustomPropertyControl : WebControl
    {
        System.Web.UI.WebControls.TextBox tb;
        
int  intNum  =   0 ;
        [Category(
" Appearance " )]
        [NumValidate(
0 10 )]
        [Description(
" 输入值范围(0~10) " )]
        
public   int  Num
        {
            
get
            {
                
return  intNum;
            }
            
set
            {
                intNum 
=  value;
            }
        }
        
protected   override   void  Render(HtmlTextWriter writer)
        {
            Table t 
=   new  Table();
            t.CellPadding 
=   0 ;
            t.CellSpacing 
=   0 ;
            TableRow tr 
=   new  TableRow();
            TableCell td_left 
=   new  TableCell();
            tb 
=   new  System.Web.UI.WebControls.TextBox();
            tb.Text 
=   this .intNum.ToString();
            td_left.Controls.Add(tb);
            tr.Controls.Add(td_left);
            NumValidateAttribute numValidateAttribute 
=   this .GetNumValidateArribute();
            
if  (numValidateAttribute.ValidateResult( this .Num)  ==   false )
            {
                TableCell td_right 
=   new  TableCell();
                System.Web.UI.WebControls.Label lb 
=   new  System.Web.UI.WebControls.Label();
                lb.ForeColor 
=  System.Drawing.Color.Red;
                lb.Text 
=   "  值输入范围必须在: "   +  numValidateAttribute.MinValue.
                ToString
                () 
+   " ~ "   +  numValidateAttribute.MaxValue.ToString()  +   " 之间! " ;
                td_right.Controls.Add(lb);
                tr.Controls.Add(td_right);
            }
            t.Controls.Add(tr);
            t.RenderControl(writer);
        }
        
private  NumValidateAttribute GetNumValidateArribute()
        {
            System.Type type 
=   this .GetType();
            PropertyInfo property 
=  type.GetProperty( " Num " );
            
object [] attrs  =  ( object [])property.GetCustomAttributes( true );
            
foreach  (Attribute attr  in  attrs)
            {
                
if  (attr  is  NumValidateAttribute)
                {
                    
return  attr  as  NumValidateAttribute;
                }
            } 
return   null ;
        }
    }
    [AttributeUsage(AttributeTargets.Property, AllowMultiple 
=   true , Inherited  =   true )]
    
public   class  NumValidateAttribute : Attribute
    {
        
///   <summary>
        
///  构造方法
        
///   </summary>
        
///   <param name="intMinValue"> 最小值 </param>
        
///   <param name="intMaxValue"> 最大值 </param>
         public  NumValidateAttribute( int  intMinValue,  int  intMaxValue)
        {
            
this .intMinValue  =  intMinValue;
            
this .intMaxValue  =  intMaxValue;
        }
        
private   int  intMinValue;
        
///   <summary>
        
///  最大值
        
///   </summary>
         public   int  MinValue
        {
            
get
            {
                
return  intMinValue;
            }
        }
        
private   int  intMaxValue;
        
///   <summary>
        
///  最小值
        
///   </summary>
         public   int  MaxValue
        {
            
get
            {
                
return  intMaxValue;
            }
        }
        
///   <summary>
        
///  执行验证
        
///   </summary>
        
///   <param name="value"></param>
        
///   <returns></returns>
         public   bool  ValidateResult( int  value)
        {
            
if  ( this .intMinValue  <=  value  &&  value  <=   this .intMaxValue)
            {
                
return   true ;
            }
            
else
            {
                
return   false ;
            }
        }
    }
}

你可能感兴趣的:(attribute)