C# Attribute-特性

Type t = typeof(TestAttribute);

            object[] attributes = t.GetCustomAttributes(false);

            foreach (var attr in attributes)
            {
                if (attr.GetType() == typeof(LogInfoAttribute))
                {
                    LogInfoAttribute att = (LogInfoAttribute)attr;
                    Console.WriteLine(String.Format("LogInfoType:{0},,OperatorName:{1},,OperatorDateTime:{2},,Mark:{3}", att.LogInfoType, att.OperatorName, att.OperatorDateTime, att.Mark));
                }
            }

            Console.WriteLine("=================================");

            MemberInfo[] members = t.GetMethods();
            foreach (var member in members)
            {
                if (member.IsDefined(typeof(LogInfoAttribute), false))
                {
                    Console.WriteLine(member.Name + "Attribute start..................");
                    object[] attrs = member.GetCustomAttributes(false);
                    foreach (var attr in attrs)
                    {
                        if (attr.GetType() == typeof(LogInfoAttribute))
                        {
                            LogInfoAttribute att = (LogInfoAttribute)attr;
                            Console.WriteLine(String.Format("LogInfoType:{0},,OperatorName:{1},,OperatorDateTime:{2},,Mark:{3}", att.LogInfoType, att.OperatorName, att.OperatorDateTime, att.Mark));
                        }
                    }
                    Console.WriteLine(member.Name + "Attribute end..................");
                }
            }

 

posted @ 2013-07-25 09:58  刘文天  阅读(153)  评论(0编辑  收藏  举报