static void Main() { Type clsType = typeof(TestClass); MethodInfo mInfo = clsType.GetMethod("TestMethod"); ParameterInfo[] pInfoArray = mInfo.GetParameters(); if (pInfoArray != null) { ArgumentUsageAttribute arrayUsageAttr1 = (ArgumentUsageAttribute) Attribute.GetCustomAttribute(pInfoArray[0], typeof(ArgumentUsageAttribute)); ArgumentUsageAttribute arrayUsageAttr2 = (ArgumentUsageAttribute) Attribute.GetCustomAttribute(pInfoArray[0], typeof(ArgumentUsageAttribute)); Console.WriteLine(" \"{0}\" == \n \"{1}\" ? {2}", arrayUsageAttr1.ToString(), arrayUsageAttr2.ToString(), arrayUsageAttr1.Equals(arrayUsageAttr2)); } }
// Create Attribute objects and compare them. static void Main() { // 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($" \"{arrayUsageAttr1}\" == \n" + $" \"{arrayUsageAttr2}\"? {arrayUsageAttr1.Equals( arrayUsageAttr2)}"); Console.WriteLine("\nCompare a usage attribute to " + "another usage attribute:"); Console.WriteLine($" \"{arrayUsageAttr1}\" == \n" + $" \"{listUsageAttr}\"? {arrayUsageAttr1.Equals(listUsageAttr)}"); Console.WriteLine("\nCompare an ID attribute instance to " + "another instance of the same attribute:"); Console.WriteLine($" \"{ arrayIDAttr1}\" == \n" + $" \"{arrayIDAttr2}\"? {arrayIDAttr1.Equals(arrayIDAttr2)}"); Console.WriteLine("\nCompare an ID attribute to another ID attribute:"); Console.WriteLine($" \"{arrayIDAttr1}\" == \n" + $" \"{listIDAttr}\"? {arrayIDAttr1.Equals(listIDAttr)}"); } else { Console.WriteLine("The parameters information could " + "not be retrieved for method {0}.", mInfo.Name); } }