using System; [AttributeUsage(AttributeTargets.All)] public class HelpAttribute: Attribute { public HelpAttribute(string url) { this.url = url; } public string Topic = null; private string url; public string Url { get { return url; } } }defines an attribute class named HelpAttribute, or Help for short, that has one positional parameter (string url) and one named parameter (string Topic). Positional parameters are defined by the formal parameters for public instance constructors of the attribute class, and named parameters are defined by public non-static read-write fields and properties of the attribute class. The example
[Help("http://www.mycompany.com/.../Class1.htm")] public class Class1 { [Help("http://www.mycompany.com/.../Class1.htm", Topic = "F")] public void F() {} }shows several uses of the attribute Help. Attribute information for a given program element can be retrieved at run-time by using reflection support. The example
using System; class Test { static void Main() { 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); } } }checks to see if Class1 has a Help attribute, and writes out the associated Topic and Url values if the attribute is present. End of informative text.
| |
Jagger Software Ltd | |
Company # 4070126 | |
VAT # 762 5213 42 |