- Reflection on Types
- Getting Started with C# Reflection
- More Advanced Uses of C# Reflection
- Reflection and Loose Coupling: Late Binding
- Conclusion
More Advanced Uses of C# Reflection
What if we now want to see the interfaces for a given type? The code in Listing 7 extracts the interfaces for a given object.
Listing7Extracting interface details.
Type[] interfaceInfo = ancillary.GetType().GetInterfaces(); foreach (Type t in interfaceInfo) Console.WriteLine("Interface name: {0}", t.Name);
The program output for the code in Listing 7 looks like this:
Interface name: IMyInterface Interface name: IMyBaseInterface
We've managed to establish that the ancillary object implements two interfaces: IMyInterface and IMyBaseInterface. Remember our interface-based inheritance hierarchy from Listing 3? In effect, Listing 7 illustrates how you use reflection to determine the details of a given class hierarchy. If you want more information, there are additional reflection services that you can use:
- Is a class sealed?
- Is a given type a base type?
These and similar services allow you to get a quite complete runtime picture of a given object. Interesting—but we're still only getting part of the picture of the true power of reflection. For the examples we've seen so far, we already have compile-time knowledge of the code in question. Now we need to look at situations in which we don't have any access to compile-time information.