• 自定义Attribute简例


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Diagnostics;
    using System.Reflection;

    namespace WebComTest
    {
        [DefaultProperty(
    "Num")]
        [ToolboxData(
    "<{0}:CustomPropertyControl runat=server></{0}:CustomPropertyControl>")]
        
    public class CustomPropertyControl : WebControl
        {
            TextBox tb;
            
    int intNum = 0;
            [Category(
    "Appearance")]
            [NumValidate(
    010)]
            [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 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();
                    Label lb 
    = new 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();//这里可根据需求修改。this可改为其它类
                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;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace WebComTest
    {
        [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;
                }
            }
        }
    }
  • 相关阅读:
    简易的sniffer程序
    ubuntu 13.10 Rhythmbox不能播放mp3 和中文乱码的问题
    5.单行函数,多行函数,字符函数,数字函数,日期函数,数据类型转换,数字和字符串转换,通用函数(case和decode)
    Java设计模式-单例模式
    WordPress的用户系统总结
    STM8S EEPROM 操作
    简化ui文件转换写法
    大型网站架构之应用服务器集群化
    大型网站架构之分布式缓存
    大型网站架构之JAVA中间件
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2061559.html
Copyright © 2020-2023  润新知