That derive from the memberinfo class we would need

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

View Full Document Right Arrow Icon
that derive from the MemberInfo class, we would need to get hold of the Type object for MemberInfo . Example 17-5 does this. Example 17-5. Using a specific type object for comparison purposes using System; using System.Linq; using System.Reflection; namespace UsingASpecificType Reflection | 681
Image of page 705

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

View Full Document Right Arrow Icon
{ public class Tester { public static void Main() { // examine a single type Assembly a = Assembly.Load("Mscorlib"); var matchingTypes = from t in a.GetTypes() where typeof(MemberInfo).IsAssignableFrom(t) select t; foreach (Type t in matchingTypes) { Console.WriteLine(t); } } } } This uses a LINQ query to find the matching types. It illustrates one of the things you can do with a Type object—its IsAssignableFrom method tells you whether it’s possible to assign an instance of one type into a field or variable of another type. So this code looks at every type, and asks whether it can be assigned into a variable of type MemberInfo . (This casts the net slightly wider than merely looking at the base class— this query will find all types that derive either directly or indirectly from MemberInfo .) Since we know exactly what target type we’re interested in, we use the C# typeof operator to get the Type object for that exact type. Finding all type members You can ask a Type object for all its members using the GetMembers() method of the Type class, which lists all the methods, properties, and fields, as shown in Example 17-6 . Example 17-6. Reflecting on the members of a type using System; using System.Reflection; namespace ReflectingOnMembersOfAType { public class Tester { public static void Main() { // examine a single type Assembly a = Assembly.Load("Mscorlib"); Type theType = a.GetType("System.Reflection.Assembly"); Console.WriteLine("\nSingle Type is {0}\n", theType); // get all the members MemberInfo[] mbrInfoArray = theType.GetMembers(); foreach (MemberInfo mbrInfo in mbrInfoArray) 682 | Chapter 17: Attributes and Reflection
Image of page 706
{ Console.WriteLine("{0} is a {1}", mbrInfo, mbrInfo.MemberType); } } } } Once again, the output is quite lengthy, but within the output you see fields, methods, constructors, and properties, as shown in this excerpt: System.Type GetType(System.String, Boolean, Boolean) is a Method System.Type[] GetExportedTypes() is a Method System.Reflection.Module GetModule(System.String) is a Method System.String get_FullName() is a Method Finding type methods You might want to focus on methods only, excluding the fields, properties, and so forth. To do so, find the call to GetMembers() : MemberInfo[] mbrInfoArray = theType.GetMembers(); and replace it with a call to GetMethods() : mbrInfoArray = theType.GetMethods(); The output now contains nothing but the methods: Output (excerpt): Boolean Equals(System.Object) is a Method System.String ToString() is a Method System.String CreateQualifiedName( System.String, System.String) is a Method Boolean get_GlobalAssemblyCache() is a Method Late Binding Once you find a method, you can invoke it using reflection. For example, you might like to invoke the Cos() method of System.Math , which returns the cosine of an angle.
Image of page 707

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

View Full Document Right Arrow Icon
Image of page 708
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