• 自定义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;
                }
            }
        }
    }
  • 相关阅读:
    EML格式解析及其访问实现
    Windows Live Writer测试
    这几天为搬房子的事烦死了。
    今天装MSSQL2005时发现有些安装文件要跟2000共享(无法更改安装盘符)
    今天看到一段比较有意思的JS脚本,根据访问速度来选择镜像。
    MS的帮助越来越多视频了。
    邹健写的公交车路线查询(包括转车近到远排列)。
    今天终于搬到这边住了,就是感觉贵了点。
    第一次看到银行系统用DotNet来做。
    这几天上火了
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2061559.html
Copyright © 2020-2023  润新知