代码: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; }         
        }     
    }
[Record("更新", "Matthew", DateTime.Now, Memo = "修改 ToString()方法")]    public partial class _Default : System.Web.UI.Page
    {        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //System.Linq.Expressions.Expression.Lambda().Compile();
              
                
                Response.Write("111");
            }
        }
    }
如果我想让Record的记录写进数据库,并且有一个插入数据库数据的方法 DAO.Insert(RecordAttribute record),请问怎么写才能达到我想要的效果?

解决方案 »

  1.   

    DAO.Insert(typeof(_Default).GetAttributes(typeof(RecordAttribut))[0])
      

  2.   

      public class Test
        {
            [Record("更新", "Matthew", "2010-8-4")]
            public void GetName()
            {
            }
        }
        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; }
            }
        }
                Type _t = typeof(Test);
                foreach (MethodInfo _mi in _t.GetMethods())
                {
                    if ("GetName" == _mi.Name)
                    {
                        object[] _o = _mi.GetCustomAttributes(typeof(RecordAttribute), false);
                        if (_o.Length > 0)
                        {
                            RecordAttribute _r = _o[0] as RecordAttribute;
                            if (null != _r)
                            {
                                Console.WriteLine(_r.Author);
                            }
                        }
                    }
                }            Console.Read();
      

  3.   

    这句话是错的,特性是描述性的行为,在编译的时候就已经生成好了
    所以你说的方式不能实现。
    例如:[obsolete] 说明方法是过时的等等