static void RuntimeUse() { Type type = typeof(Class1); object[] arr = type.GetCustomAttributes(typeof(HelpAttribute), true); if (arr.Length == 0) { Console.WriteLine("Class1 has no Help attribute."); } else { HelpAttribute ha = (HelpAttribute)arr[0]; Console.WriteLine("Url = {0}, Topic = {1}", ha.Url, ha.Topic); } }
public static void Usage() { Type widgetType = typeof(Widget); object[] widgetClassAttributes = widgetType.GetCustomAttributes(typeof(HelpAttribute), false); if (widgetClassAttributes.Length > 0) { HelpAttribute attr = (HelpAttribute)widgetClassAttributes[0]; Console.WriteLine($"Widget class help URL: {attr.Url} - Related topic: {attr.Topic}"); } System.Reflection.MethodInfo displayMethod = widgetType.GetMethod(nameof(Widget.Display)); object[] displayMethodAttributes = displayMethod.GetCustomAttributes(typeof(HelpAttribute), false); if (displayMethodAttributes.Length > 0) { HelpAttribute attr = (HelpAttribute)displayMethodAttributes[0]; Console.WriteLine($"Display method help URL: {attr.Url} - Related topic: {attr.Topic}"); } }