自定义Attribute的作用

时间:2021-07-25 23:15:23
预定义的Attribute用法都显而易见,不明白自定义的Attribute的具体作用在哪里,
我看到的简单的例子:

    //自定义 Attribute, to be assigned to a class and its members
    [AttributeUsage(
       AttributeTargets.Class |
       AttributeTargets.Constructor |
       AttributeTargets.Field |
       AttributeTargets.Method |
       AttributeTargets.Property,
       AllowMultiple = true)]
    class DeBugInfo : Attribute
    {
        private int bugNo;
        private string developer;
        private string lastReview;
        public string message;

        public DeBugInfo(int bg, string dev, string d)
        {
            this.bugNo = bg;
            this.developer = dev;
            this.lastReview = d;
        }
        public int BugNo
        {
            get
            {
                return bugNo;
            }
        }
        public string Developer
        {
            get
            {
                return developer;
            }
        }
        public string LastReview
        {
            get
            {
                return lastReview;
            }
        }
        public string Message
        {
            get
            {
                return message;
            }
            set
            {
                message = value;
            }
        }
    }

    [DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
    [DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")]
    class Rectangle
    {
        protected double length;
        protected double width;

        public Rectangle(double l, double w)
        {
            length = l;
            width = w;
        }
        [DeBugInfo(55, "Zara Ali", "19/10/2012", Message = "Return type mismatch")]
        public double GetArea()
        {
            return length * width;
        }
        [DeBugInfo(56, "Zara Ali", "19/10/2012", Message = "xxxxxxxxxxx")]
        public void Display()
        {
            Console.WriteLine("--------- Display -------");
            Console.WriteLine("Length: {0}", length);
            Console.WriteLine("Width: {0}", width);
            Console.WriteLine("Area: {0}", GetArea());
        }
    }

    // 下面是含有Main 的 Program.cs
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle(4.5, 7.5);
            r.Display();
            Type type = typeof(Rectangle);

            Console.WriteLine("---------- The following line is attribute ---------");
            //iterating through the attribtues of the Rectangle class
            foreach (Object attributes in type.GetCustomAttributes(false))
            {
                DeBugInfo dbi = (DeBugInfo)attributes;
                if (null != dbi)
                {
                    Console.WriteLine("Bug no: {0}", dbi.BugNo);
                    Console.WriteLine("Developer: {0}", dbi.Developer);
                    Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
                    Console.WriteLine("Remarks: {0}", dbi.Message);
                }
            }
            Console.WriteLine("---------- The following line is methodinfo ---------");
            //iterating through the method attribtues
            foreach (MethodInfo m in type.GetMethods())
            {
                foreach (Attribute a in m.GetCustomAttributes(true))
                {
                    if (a is DeBugInfo)
                    {
                        DeBugInfo dbi = (DeBugInfo)a;
                        if (null != dbi)
                        {
                            Console.WriteLine("Bug no: {0}, for Method: {1}", dbi.BugNo, m.Name);
                            Console.WriteLine("Developer: {0}", dbi.Developer);
                            Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
                            Console.WriteLine("Remarks: {0}", dbi.Message);
                        }
                    }
                }
            }
            Console.ReadLine();
        }
    }




输出结果:

--------- Display -------
Length: 4.5
Width: 7.5
Area: 33.75
---------- The following line is attribute ---------
Bug no: 45
Developer: Zara Ali
Last Reviewed: 12/8/2012
Remarks: Return type mismatch
Bug no: 49
Developer: Nuha Ali
Last Reviewed: 10/10/2012
Remarks: Unused variable
---------- The following line is methodinfo ---------
Bug no: 55, for Method: GetArea
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks: Return type mismatch
Bug no: 56, for Method: Display
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks: xxxxxxxxxxx


看起来不过就是将加到代码里的4行:

    [DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
    [DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")]
    [DeBugInfo(55, "Zara Ali", "19/10/2012", Message = "Return type mismatch")]
    [DeBugInfo(56, "Zara Ali", "19/10/2012", Message = "xxxxxxxxxxx")]

重新提取了出来,这有什么意义啊?

6 个解决方案

#1


    特性在 .Net  MVC 里面是 AOP编程的切入点和重要的组成部分。

    是筛选器实现的前提。 
  

#3


相当于类型的“说明书”,告诉动态调用(反射)这些类型的程序,这个类型的一些信息。

#4


这只是个案例吧?教你怎么使用特性:
1、怎么定义特性
2、Main方法中演示,怎么使特性生效(特性怎么生效,在哪里生效,是要写代码实现的)

#5


实际使用特性,当然不会在Main方法中这样去写,那没有意义。
这只是告诉你,当你要使用特性时,是怎么使用的,你是要先使用typeof获得type,
然后type.GetCustomAttributes(false)得到特性,然后才能调用特性的属性和方法。

#6


引用 楼主 ftell 的回复:
预定义的Attribute用法都显而易见,不明白自定义的Attribute的具体作用在哪里,


这是典型的初学时盲目问题。既然预定义的用法显而易见,而代码都是人写的,为什么自己就不能写?只有外国人才能写代码而自己(中国人)不能写代码?(这里把微软的工程师偷换概念为“外国人”)

“如果系统预定义的特性不够用,那么就自己动手写一个”。这其实是一个程序员初步学懂点软件的标志之一。如果这个理念都不理解,那么说多少技术都白搭。需要自己强大起来,你才不至于以为只有外国人才配写代码。

#1


    特性在 .Net  MVC 里面是 AOP编程的切入点和重要的组成部分。

    是筛选器实现的前提。 
  

#2


#3


相当于类型的“说明书”,告诉动态调用(反射)这些类型的程序,这个类型的一些信息。

#4


这只是个案例吧?教你怎么使用特性:
1、怎么定义特性
2、Main方法中演示,怎么使特性生效(特性怎么生效,在哪里生效,是要写代码实现的)

#5


实际使用特性,当然不会在Main方法中这样去写,那没有意义。
这只是告诉你,当你要使用特性时,是怎么使用的,你是要先使用typeof获得type,
然后type.GetCustomAttributes(false)得到特性,然后才能调用特性的属性和方法。

#6


引用 楼主 ftell 的回复:
预定义的Attribute用法都显而易见,不明白自定义的Attribute的具体作用在哪里,


这是典型的初学时盲目问题。既然预定义的用法显而易见,而代码都是人写的,为什么自己就不能写?只有外国人才能写代码而自己(中国人)不能写代码?(这里把微软的工程师偷换概念为“外国人”)

“如果系统预定义的特性不够用,那么就自己动手写一个”。这其实是一个程序员初步学懂点软件的标志之一。如果这个理念都不理解,那么说多少技术都白搭。需要自己强大起来,你才不至于以为只有外国人才配写代码。