Memberinfo is defined in the systemreflection

Info icon This preview shows pages 702–705. Sign up to view the full content.

View Full Document Right Arrow Icon
MemberInfo is defined in the System.Reflection namespace. We can use it to discover the attributes of a member and to provide access to the metadata. We’ll start by getting hold of the metadata for a particular type: System.Reflection.MemberInfo inf = typeof(MyMath); We’re using the typeof operator on the MyMath type, which returns an object of type Type , which derives from MemberInfo . The Type class is the heart of the reflection classes. Type encapsulates a representation of the type of an object. The Type class is the primary way to access metadata—we can use it to get hold of information about the other members of a class (e.g., methods, properties, fields, events, etc.). The next step is to call GetCustomAttributes on this MemberInfo object, passing in the type of the attribute we want to find. It returns an array of objects, each of type BugFixAttribute : object[] attributes; attributes = inf.GetCustomAttributes(typeof(BugFixAttribute),false); You can now iterate through this array, printing out the properties of the BugFixAttri bute object. Example 17-2 replaces the Main method in the Tester class from Example 17-1 . 678 | Chapter 17: Attributes and Reflection
Image of page 702

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example 17-2. Using reflection public static void Main(string[] args) { MyMath mm = new MyMath(); Console.WriteLine("Calling DoFunc(7). Result: {0}" , mm.DoFunc1(7)); // get the member information and use it to // retrieve the custom attributes System.Reflection.MemberInfo inf = typeof(MyMath); object[] attributes; attributes = inf.GetCustomAttributes( typeof(BugFixAttribute), false); // iterate through the attributes, retrieving the // properties foreach (Object attribute in attributes) { BugFixAttribute bfa = (BugFixAttribute)attribute; Console.WriteLine("\nBugID: {0}", bfa.BugID); Console.WriteLine("Programmer: {0}", bfa.Programmer); Console.WriteLine("Date: {0}", bfa.Date); Console.WriteLine("Comment: {0}", bfa.Comment); } } Output: Calling DoFunc(7). Result: 9.3333333333333333 BugID: 121 Programmer: Jesse Liberty Date: 01/03/08 Comment: BugID: 107 Programmer: Jesse Liberty Date: 01/04/08 Comment: Fixed off by one errors When you put this replacement code into Example 17-1 and run it, you can see the metadata printed as you’d expect. Type Discovery You can use reflection to explore and examine the contents of an assembly. You can find the types it contains. You can discover the methods, fields, properties, and events associated with a type, as well as the signatures of each of the type’s methods. You can also discover the interfaces supported by the type, and the type’s base class. If we were using this to support a plug-in system for extending our application, we’d need to load at runtime assemblies we didn’t know about when we wrote our applica- tion. We can load an assembly dynamically with the Assembly.Load() static method. Reflection | 679
Image of page 703
The Assembly class encapsulates the actual assembly itself, for purposes of reflection.
Image of page 704

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 705
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern