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..................");
                }
            }
原文地址:https://www.cnblogs.com/liuwentian/p/3213716.html