Describing a .NET type

For custom .NET types that you want Powershell to manipulate, you need to provide some display format information. This information tells Powershell how to display your type in tables and lists so that it looks good. For example, you can specify what properties to display and which ones to omit in a regular "Format-Table" output, and how to format each column. Without this information, Powershell will display your type in a rather ugly list view, printing all properties.

To describe a .NET type so that PoshBuild can generate a display format file, you need to do the following.
  • Add a DisplayFormatAttribute (from PoshBuild) on your .NET type. If a type doesn't have this attribute, nothing will be generated for this type in the display format file. This means Powershell will print it using the ugly default list view.
  • Add a DisplayFormatColumnAttribute (from PoshBuild) on the properties of your .NET type.

Using IDisplayFormatDescriptors

The problem with the above solution is that you will soon end up with a dependency on PoshBuild.dll, which is not desirable. You might even not have access to the code for the .NET types you want to use in Powershell, and you can't update their assemblies. To decouple this, you can create display format descriptors, which will provide those attributes (DisplayFormat, DisplayFormatColumn, etc.) on behalf of your .NET type, and compile them as a separate assembly. Think of them as a simple, specific version of custom type descriptors.

To define a display format descriptor, just create a class that implements the IDisplayFormatDescriptor interface, and apply the DisplayFormatDescriptorAttribute on it, specifying what type this descriptor describes. The rest should be straightforward.

Last edited Jul 17, 2008 at 3:12 AM by ludovic_chabant, version 1


No comments yet.