How it works...

Reflection provides .NET objects that contain information about assemblies and data types. While this in itself is already useful, reflection can also be used to dynamically generate new data types at runtime or to change otherwise read-only properties of objects, also known as fields. Fields are usually referred to when we talk about private class members that can neither be read nor set from outside the object. To read or also change field values, they are often wrapped in properties that allow access to get and set methods. A set method is also a great place to apply some additional validation before a field is modified.

Using reflection opens up a whole new world, with even more things that would normally not be possible. Even if you do not regularly modify private fields, knowing how to use Add-Member to simply add members is a great skill. The Add-Member cmdlet allows you to easily extend existing objects so that they contain more information temporarily.