Esempio n. 1
0
        // Create Attribute objects and compare them.
        static void Main( )
        {
            Console.WriteLine("This example of Attribute.Equals( object ) " +
                              "generates the following output.");

            // Get the class type, and then get the MethodInfo object
            // for TestMethod to access its metadata.
            Type       clsType = typeof(TestClass);
            MethodInfo mInfo   = clsType.GetMethod("TestMethod");

            // There will be two elements in pInfoArray, one for each parameter.
            ParameterInfo[] pInfoArray = mInfo.GetParameters();
            if (pInfoArray != null)
            {
                // Create an instance of the argument usage attribute on strArray.
                ArgumentUsageAttribute arrayUsageAttr1 = (ArgumentUsageAttribute)
                                                         Attribute.GetCustomAttribute(pInfoArray[0],
                                                                                      typeof(ArgumentUsageAttribute));

                // Create another instance of the argument usage attribute
                // on strArray.
                ArgumentUsageAttribute arrayUsageAttr2 = (ArgumentUsageAttribute)
                                                         Attribute.GetCustomAttribute(pInfoArray[0],
                                                                                      typeof(ArgumentUsageAttribute));

                // Create an instance of the argument usage attribute on strList.
                ArgumentUsageAttribute listUsageAttr = (ArgumentUsageAttribute)
                                                       Attribute.GetCustomAttribute(pInfoArray[1],
                                                                                    typeof(ArgumentUsageAttribute));

                // Create an instance of the argument ID attribute on strArray.
                ArgumentIDAttribute arrayIDAttr1 = (ArgumentIDAttribute)
                                                   Attribute.GetCustomAttribute(pInfoArray[0],
                                                                                typeof(ArgumentIDAttribute));

                // Create another instance of the argument ID attribute on strArray.
                ArgumentIDAttribute arrayIDAttr2 = (ArgumentIDAttribute)
                                                   Attribute.GetCustomAttribute(pInfoArray[0],
                                                                                typeof(ArgumentIDAttribute));

                // Create an instance of the argument ID attribute on strList.
                ArgumentIDAttribute listIDAttr = (ArgumentIDAttribute)
                                                 Attribute.GetCustomAttribute(pInfoArray[1],
                                                                              typeof(ArgumentIDAttribute));

                // Compare various pairs of attributes for equality.
                Console.WriteLine("\nCompare a usage attribute instance to " +
                                  "another instance of the same attribute:");
                Console.WriteLine("   \"{0}\" == \n   \"{1}\" ? {2}",
                                  arrayUsageAttr1.ToString(), arrayUsageAttr2.ToString(),
                                  arrayUsageAttr1.Equals(arrayUsageAttr2));

                Console.WriteLine("\nCompare a usage attribute to " +
                                  "another usage attribute:");
                Console.WriteLine("   \"{0}\" == \n   \"{1}\" ? {2}",
                                  arrayUsageAttr1.ToString(), listUsageAttr.ToString(),
                                  arrayUsageAttr1.Equals(listUsageAttr));

                Console.WriteLine("\nCompare an ID attribute instance to " +
                                  "another instance of the same attribute:");
                Console.WriteLine("   \"{0}\" == \n   \"{1}\" ? {2}",
                                  arrayIDAttr1.ToString(), arrayIDAttr2.ToString(),
                                  arrayIDAttr1.Equals(arrayIDAttr2));

                Console.WriteLine("\nCompare an ID attribute to another ID attribute:");
                Console.WriteLine("   \"{0}\" == \n   \"{1}\" ? {2}",
                                  arrayIDAttr1.ToString(), listIDAttr.ToString(),
                                  arrayIDAttr1.Equals(listIDAttr));
            }
            else
            {
                Console.WriteLine("The parameters information could " +
                                  "not be retrieved for method {0}.", mInfo.Name);
            }
        }