C#自定义特性

code1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyLibrary.AttributeClass
{
    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
    public class RecordAttribute : Attribute
    {
        private string recordType;      // 记录类型:更新/创建    
        private string author;          // 作者    
        private DateTime date;          // 更新/创建 日期    
        private string memo;         // 备注    

        // 构造函数,构造函数的参数在特性中也称为“位置参数”。    
        public RecordAttribute(string recordType, string author, string date)
        {
            this.recordType = recordType;
            this.author = author;
            this.date = Convert.ToDateTime(date);
        }

        // 对于位置参数,通常只提供get访问器    
        public string RecordType { get { return recordType; } }
        public string Author { get { return author; } }
        public DateTime Date { get { return date; } }

        // 构建一个属性,在特性中也叫“命名参数”    
        public string Memo
        {
            get { return memo; }
            set { memo = value; }
        }
    }

    [RecordAttribute("modify", "felix", "2010-11-09")]
    [RecordAttribute("create", "felix", "2010-11-08")]
    public class TestAttribute
    {
        public string GetInfo() {
            return "hello world";
        }
        public string GetInfos() {
            return "hello attribute";
        }
    }
}

code2:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using MyLibrary.AttributeClass;
    using System.Reflection;

    public partial class AttributePart_AttributeView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ShowAttributes("TestAttribute");
        }

        public void ShowAttributes()
        {
            Type type = typeof(TestAttribute);

            object[] records = type.GetCustomAttributes(typeof(RecordAttribute), false);

            foreach (RecordAttribute record in records)
            {
                Response.Write(record);
                Response.Write("类型:" + record.RecordType);
                Response.Write("作者:" + record.Author);
                Response.Write("日期:" + record.Date.ToString());
            }
        }

        public void ShowAttributes(string typeName)
        {
            Assembly a = Assembly.Load("MyLibrary");

            Object obj = a.CreateInstance("MyLibrary.AttributeClass." + typeName);

            Type type = obj.GetType();

            //get all attributes of TestAttribute
            object[] records = type.GetCustomAttributes(typeof(RecordAttribute), false);

            foreach (RecordAttribute record in records)
            {
                Response.Write(record);
                Response.Write("类型:" + record.RecordType);
                Response.Write("作者:" + record.Author);
                Response.Write("日期:" + record.Date.ToString());
            }

            //get and invoke all methods of TestAttribute
            try
            {
                //MethodInfo[] mi = type.GetMethods();
               
                Response.Write(type.GetMethod("GetInfo").Invoke(obj, null));

                Response.Write(type.GetMethod("GetInfos").Invoke(obj, null));
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
                throw ex;
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值