13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8 <strong>Language</strong> Overview}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 theformal parameters for public instance constructors of the attribute class, and named parameters are definedby 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 exampleusing 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 theattribute is present.End of informative text.49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!