static void ShowAttributeTypeIds( ) { // 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 param array attribute on strList. ParamArrayAttribute listArrayAttr = (ParamArrayAttribute) Attribute.GetCustomAttribute(pInfoArray[1], typeof(ParamArrayAttribute)); // 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)); // Display the attributes and corresponding TypeId values. Console.WriteLine("\n\"{0}\" \nTypeId: {1}", listArrayAttr.ToString(), listArrayAttr.TypeId); Console.WriteLine("\n\"{0}\" \nTypeId: {1}", arrayUsageAttr1.ToString(), arrayUsageAttr1.TypeId); Console.WriteLine("\n\"{0}\" \nTypeId: {1}", arrayUsageAttr2.ToString(), arrayUsageAttr2.TypeId); Console.WriteLine("\n\"{0}\" \nTypeId: {1}", listUsageAttr.ToString(), listUsageAttr.TypeId); } else { Console.WriteLine("The parameters information could " + "not be retrieved for method {0}.", mInfo.Name); } }
// 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); } }