Chapter 26 Reflection

What’s in This Chapter

  • Learning about assemblies
  • Enumerating fields, properties, methods, and events
  • Getting and setting property values
  • Creating add-ins
  • Compiling and executing scripts at run time

Wrox.com Downloads for This Chapter

Please note that all the code examples for this chapter are available as a part of this chapter’s code download on the book’s website at www.wrox.com/go/csharp5programmersref on the Download Code tab.

Reflection is a process by which a program can examine and manipulate program objects at run time. For example, serialization (described in the preceding chapter) uses reflection to figure out what values an object has and what their data types are so that it can save and restore them. IntelliSense also uses reflection to describe the parameters that a method takes while you are entering those parameters.

Reflection is a fairly advanced technique that isn’t necessary for most programs. Usually, when you write a program, you know what you want it to do and what methods you need to call to do it. It’s unusual, for example, to be working with an Invoice class and not know what properties, methods, and events that class defines. Even if you don’t know what’s in a class and you use reflection to find out, it would be quite hard to make the program use those discovered items effectively.

Still there are a few situations in which reflections can be useful. Some of the more common uses of reflection include

  • Load assemblies at run ...

Get C# 5.0 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.