Free mag vol1 | Page 640

CHAPTER 15  TYPE REFLECTION, LATE BINDING, AND ATTRIBUTE-BASED PROGRAMMING The Obsolete Attribute in Action Now that HorseAndBuggy has been marked as obsolete, if you were to allocate an instance of this type: static void Main(string[] args) { HorseAndBuggy mule = new HorseAndBuggy(); } you would find that the supplied string data is extracted and displayed within the Error List window of Visual Studio, as well as on the offending line of code when you hover your mouse cursor above the obsolete type (see Figure 15-4). Figure 15-4. Attributes in action In this case, the “other piece of software” that is reflecting on the [Obsolete] attribute is the C# compiler. Hopefully, at this point, you should understand the following key points regarding .NET attributes: • Attributes are classes that derive from System.Attribute. • Attributes result in embedded metadata. • Attributes are basically useless until another agent reflects upon them. • Attributes are applied in C# using square brackets. Next up, let’s examine how you can build your own custom attributes and a piece of custom software that reflects over the embedded metadata.  Source Code The ApplyingAttributes project is included in the Chapter 15 subdirectory. 582