14.4. Using .NET Reflection

When you use Windows PowerShell to work with .NET Framework objects, you can take advantage of the functionality of .NET reflection. Reflection allows you to look inside an assembly and find out its characteristics. Inside a .NET assembly information is stored that describes what the assembly contains. This is called metadata. A .NET assembly is, in a sense, self-describing, at least if interrogated correctly.

In the .NET Framework, assemblies contain modules, modules contain classes, and classes contain members. Reflection allows you to explore the hierarchy of assemblies, modules, classes, and members.

The System.Reflection namespace, on which .NET reflection is based, is an extensive one. A full discussion of reflection is beyond the scope of this chapter, but the following introduction should help you get started so that you can, from Windows PowerShell, explore in detail the characteristics of .NET classes and types that interest you.

Reflection uses the members of the System.Reflection namespace together with System.Type. In .NET a namespace is simply a named collection.

In the following sections, I introduce several methods that you can use to find detailed information about members, methods, and properties of a .NET class. As the following sections make clear, the method you use will depend on your existing knowledge about the class. These methods can be combined with use of the get-member cmdlet and the GetType() method, if you are starting ...

Get Professional Windows® PowerShell 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.