Prev | Current Page 189 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"


We??™ll now go from compile-time efficiency to execution-time flexibility: our final
advanced topic is reflection. Even in .NET 1.0/1.1 reflection could be a little tricky,
but generic types and methods introduce an extra level of complexity. The framework
provides everything we need (with a bit of helpful syntax from C# 2 as a language),
and although the additional considerations can be daunting, it??™s not too bad if you
take it one step at a time.
3.4.4 Reflection and generics
Reflection is used by different people for all sorts of things. You might use it for
execution-time introspection of objects to perform a simple form of data binding. You
might use it to inspect a directory full of assemblies to find implementations of a plugin
interface. You might write a file for an Inversion of Control9 framework to load and
dynamically configure your application??™s components. As the uses of reflection are so
diverse, I won??™t focus on any particular one but give you more general guidance on performing
common tasks. We??™ll start by looking at the extensions to the typeof operator.
USING TYPEOF WITH GENERIC TYPES
Reflection is all about examining objects and their types. As such, one of the most
important things you need to be able to do is obtain a reference to the System.Type
object, which allows access to all the information about a particular type.


Pages:
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201