Join Ted Neward for an in-depth discussion in this video Reflection overview, part of CLR Reflection for Developers.
- [Instructor] So, in this course, we're going to be talking about .NET reflection, which is a facility of the Common Language Runtime and its ilk to allow programmers at Runtime to access type metadata. That is to say, information about the types, that is to say, the classes, the structures, the objects, etc. that you're working with. Now, in some respects, as a developer, you are already familiar with types because this is typically what you are creating when you sit down at Visual Studio and type class Foo, you are creating type information.
Historically, that information has only been present during compile time, meaning, the C# compiler or the Visual Basic compiler, will look at the types that you've created and see the code that you've worked with and make sure that the code is, in fact, obeying the type signatures and so on and so on. What Reflection does is it gives us access to that type information at Runtime so that you can write managed code that can look at a class or, that is to say, look at an object, determine its class, and then be able to determine what fields and methods and properties are available on it.
Now, one of the things to be clear is that, in some other languages, what they call Reflection actually allows you to modify that type information at Runtime, and that's something that the CLR will support, at least not in its current incarnations. Once defined, type information is effectively immutable. So most of what we're working with here is going to be read-only. However, as we'll see, Reflection can also allow you to interact with object instances, that is to say, you can use Reflection to walk up to any arbitrary object inside of the CLR and be able to interact with its properties, be able to invoke methods, be able to construct objects via a type constructor, without necessarily having to know that type name ahead of time, entirely through the Reflection API.
This what allows things like CLR serialization, remoting, the various object relational mapping tools such as Entity Framework or NHibernate. This is what allows them to be able to manipulate objects without knowing about those types ahead of time. All of this is available to you through Reflection, and, in many cases, depending upon the code that you're writing and depending upon the security settings that the CLR is operating under, will even allow you to reach past access barriers such as the private keyword from C# to be able access the values of fields.
Reflection is a very powerful API, and, in order to be able to full demonstrate it, one of the first things we're going to need to do is we're going to need to have some code that we can work with.
- Working with the IL Disassembler (Ildasm)
- Namespaces and types
- Type API
- Properties API
- MethodBase and MethodInfo API
- Constructors API
- Fields API
- Constructing objects
- Accessing properties and fields
- Invoking methods