C#中的Attribute二

一、定义特性

//字段属性约束,在定的的特性上使用系统特性
    [AttributeUsage(AttributeTargets.All,AllowMultiple =true,Inherited =true)]
   public class ShowAttribute:Attribute
    {   
        public string ShowInfo { get; set; }
        public void Show()
        {
            Console.WriteLine(ShowInfo);
        }
    }

二、使用特性

 [Show(ShowInfo = "我是在类上的第一个特性")]
    [Show(ShowInfo = "我是在类上的第二个特性")]
    public class ShowTest
    {

        [Show(ShowInfo = "我是在方法上的特性")]//通过特性类的属性进行传入值
        public void TestMethod()
        {

        }

        [Show(ShowInfo = "我是在属性上的特性")]
        public string TestProperty { get; set; }

        [Show(ShowInfo = "我是在字段上的特性")]
        public string TestFiled;
    }

三、调用特性实现

   public static class InvokeCenter
    {

        //此处使用的是拓展方法
        public static void InvokeManager<T>( this T showTest) where T:new()//泛型无参构造约束
        {
            Type type = showTest.GetType();
            if (type.IsDefined(typeof(ShowAttribute),true))
            {
                //在类上面查找特性
                object[] attributes=type.GetCustomAttributes(typeof(ShowAttribute), true);
                foreach (ShowAttribute attribute in attributes)
                {
                    attribute.Show();
                }

                //在方法上查找
                foreach (MethodInfo method in type.GetMethods())
                {
                    if (method.IsDefined(typeof(ShowAttribute), true))
                    {
                        object[] attributeMethods = method.GetCustomAttributes(typeof(ShowAttribute), true);
                        foreach (ShowAttribute attribute in attributeMethods)
                        {
                            attribute.Show();
                        }
                    }
                }

                //在属性上查找
                foreach (PropertyInfo property in type.GetProperties())
                {
                    if (property.IsDefined(typeof(ShowAttribute), true))
                    {
                        object[] attributeProperty = property.GetCustomAttributes(typeof(ShowAttribute), true);
                        foreach (ShowAttribute attribute in attributeProperty)
                        {
                            attribute.Show();
                        }
                    }
                }

                //在字段上查找
                foreach (FieldInfo field in type.GetFields())
                {
                    if (field.IsDefined(typeof(ShowAttribute), true))
                    {
                        object[] attributeField = field.GetCustomAttributes(typeof(ShowAttribute), true);
                        foreach (ShowAttribute attribute in attributeField)
                        {
                            attribute.Show();
                        }
                    }
                }


            }
        }

    }

四、调用测试

通过拓展方法调用

 ShowTest showTest = new ShowTest();
 showTest.InvokeManager();
posted @ 2022-03-29 10:42  码农阿亮  阅读(77)  评论(0编辑  收藏  举报