02 Aug Practical Reflection in .NET
Reflection is an extremely powerful feature of .NET, but it’s also one of those tools that developers look at and say, when would I ever use that? And I don’t disagree with that.
When I first saw Reflection, I thought it was really cool and also a bit scary. But I also thought that I would never have a use for it in my applications. It looked like a tool for people who create decompilers and other developer tools. It turned out I was wrong. Even though I don’t use many of the features of Reflection, there are several features that I found to be extremely useful and those features are great to have in the toolbox.
We’ll be focusing on the Practical parts of Reflection, the things that everyday programmers will find useful.
Used appropriately, Reflection can add flexibility to our applications fairly easily. But in doing this, we will put an emphasis on safety. With great power comes great responsibility.
Things you can do with Reflection
There are a lot of things we can do with Reflection, a whole lot of things, but many of these things such as manipulating private members are unwise and may cause us pain the future.
We’ll also be considering Performance. Reflection is extremely slow, compared to making statically bound calls
We’ll look at some techniques that allow us to get the flexibility of Reflection without sacrificing speed.
Everything is given and take and we’re looking to find a good balance among the Flexibility, Safety, and Performance.
What we will learn here
Here’s the path that we’ll take on our Journey. We’ll start with a high-level overview of Reflection.
We’ll touch on the features and talk about the various capabilities. This will take us into some of the geekier neighborhoods of .NET.
Reflection has a lot of very interesting features and for people who want to know how things work internally, it’s great to be able to peek inside. These features are extremely powerful and a little bit dangerous.
We’ll look at the drawbacks to using this functionality and come up with some best practices that will let us use it safely and effectively. We’ll put those best practices into action in some sample applications.
In the first, we’ll use Reflection to load types at runtime.
The specific types do not need to be present at compiler time and we just need to add some configuration when we want to swap things out. This means that we can have an application that can be easily extended, or customized for our clients. Then we’ll add some Generic Types to the application and see how that impacts the way we Reflection.
In our next example, we’ll take this a step further. Rather than configuring which types to load, our application will look through a folder of assemblies, discover which types are available, and then load up the types that support a specify function. After this, we’ll look at some Developer Tools that use Reflection including the Visual Studio Object Browser, disassemblers, and decompilers, mocking libraries, and Dependency Injection Containers.
This will help us understand how some of our tools work and this will also show us that there are a lot of great tools already out there. So there’s often no need for us to build that functionality ourselves.
Finally, we’ll alleviate some fears. After seeing what Reflection is capable of, it’s easy to become paranoid about our code. Nothing is secret and this may make us want to give up using .NET altogether.
But there are some things we can do about this. First, we need to look at things a little more rationally. Things really aren’t much more exposed than in other environments and we’ll think about some best practices to keep our confidential items confidential.
You don’t need any previous experience with Reflection or digging into assemblies or an intermediate language.
We’ll cover the things we need, as we go along. This course does assume that you have a good understanding of the basics of C# and .NET including things like Classes, Methods, Properties, Fields, and Access Modifiers.
If you’ve been working with .NET for about a year or so, you’ll follow along just fine.
So let’s get started on our journey.