Extended Type System (ETS)
Relevant source files
The following files were used as context for generating this wiki page:
- src/System.Management.Automation/engine/TypeTable.cs
- src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
- test/powershell/engine/ETS/TypeTable.Tests.ps1
- src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs
- src/System.Management.Automation/help/MamlCommandHelpInfo.cs
The Extended Type System (ETS) is a foundational component of PowerShell that allows for the dynamic extension of .NET objects. It enables developers to attach new properties, methods, and metadata to existing types without modifying the underlying class definitions. This is achieved by wrapping base objects in a PSObject (internally referenced as MshObject in legacy contexts) and maintaining a global TypeTable that maps type hierarchies to extended members.
ETS is primarily responsible for object adaptation, type conversion, and providing the metadata required for PowerShell's formatting and serialization systems. It allows PowerShell to treat disparate data sources—such as XML, WMI, or .NET objects—as a unified set of objects with consistent member access.
Sources: src/System.Management.Automation/engine/TypeTable.cs, src/System.Management.Automation/help/MamlCommandHelpInfo.cs:64-75
Core Architecture and TypeTable
The TypeTable class is the central repository for all type extensions within a PowerShell session. It caches information parsed from types.ps1xml files and dynamic TypeData instances added via cmdlets.
Member Consolidation Logic
When a member is requested for an object, ETS performs a "consolidation" process. It traverses the object's type hierarchy (represented by the TypeNames collection) and retrieves extended members from the TypeTable. More specific types in the hierarchy can override members defined by more general base types.
Sources: src/System.Management.Automation/engine/TypeTable.cs:1757-1770, src/System.Management.Automation/engine/TypeTable.cs:1817-1830
The following diagram illustrates the relationship between objects, the TypeTable, and the resulting member collection:
This diagram shows how the PSObject wrapper uses the type hierarchy to look up and consolidate extended members from the TypeTable.
Sources: src/System.Management.Automation/engine/TypeTable.cs:1757-1768
Key Classes
- TypeData: Represents a comprehensive set of extensions for a single type, including members, type converters, and adapters.
- TypeMemberData: The abstract base class for all extended member definitions.
- ConsolidatedString: An internal helper class used as a key in
TypeTablecaches, representing a joined string of an object's type hierarchy.
Sources: src/System.Management.Automation/engine/TypeTable.cs:1340-1365, src/System.Management.Automation/engine/TypeTable.cs:1225-1250
Type Member Definitions
ETS supports several distinct types of extended members, defined through subclasses of TypeMemberData. These are typically loaded from XML configuration or constructed programmatically.
Member Categories
| Member Type | Class | Description |
|---|---|---|
| NoteProperty | NotePropertyData | A static value attached to a type. |
| AliasProperty | AliasPropertyData | A property that points to another existing member. |
| ScriptProperty | ScriptPropertyData | A property whose value is determined by a getter/setter ScriptBlock. |
| CodeProperty | CodePropertyData | A property mapped to static .NET methods for its getter and setter. |
| ScriptMethod | ScriptMethodData | A method implemented as a ScriptBlock. |
| CodeMethod | CodeMethodData | A method mapped to a static .NET method. |
| PropertySet | PropertySetData | A collection of property names (e.g., DefaultDisplayPropertySet). |
| MemberSet | MemberSetData | A named grouping of multiple TypeMemberData instances. |
Sources: src/System.Management.Automation/engine/TypeTable.cs:1446-1600, test/powershell/engine/ETS/TypeTable.Tests.ps1:24-60
Type Member Class Hierarchy
This diagram represents the inheritance structure used by ETS to define various extended members.
Sources: src/System.Management.Automation/engine/TypeTable.cs:1446-1620
Standard Members and Serialization Metadata
ETS reserves a special MemberSet named PSStandardMembers to store metadata used by the PowerShell engine for internal operations.
Key Internal Metadata Properties
- DefaultDisplayPropertySet: Defines which properties are shown by default in formatting views.
- SerializationMethod: Determines if an object is serialized as a string, by its public properties, or by a specific property set.
- SerializationDepth: Specifies how deep the serialization engine should traverse nested objects.
- InheritPropertySerializationSet: A boolean indicating if sub-types should inherit the serialization settings of their parents.
- TargetTypeForDeserialization: Informs the engine which type to use when recreating the object from a serialized state.
Sources: src/System.Management.Automation/engine/TypeTable.cs:1203-1222, src/System.Management.Automation/engine/TypeTable.cs:1920-2050, test/powershell/engine/ETS/TypeTable.Tests.ps1:78-90
Manifest and Module Integration
Modules can distribute ETS extensions via the TypesToProcess field in a module manifest (.psd1).
Processing Logic
When Import-Module is called, the ModuleCmdletBase class handles the resolution and loading of .ps1xml files. These files are processed by the TypesPs1xmlReader and added to the TypeTable. If a module is removed, its associated type extensions are purged to maintain session integrity.
Sources: src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs:1571-1600, src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs:2287-2300
The flow shows how PowerShell integrates ETS definitions during module import, ensuring that new types are available in the current runspace.
Sources: src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs:1571-1585, src/System.Management.Automation/engine/TypeTable.cs:2720-2750
Type Converters and Adapters
ETS provides extensibility points for type conversion and property adaptation through the TypeTable.
- TypeConverters: Map specific types to
PSTypeConverteror standardTypeConverterinstances. This allows PowerShell to convert strings or other objects into complex types during parameter binding or variable assignment. - TypeAdapters: Used to provide a property-like interface to data structures that do not naturally have properties, such as
DataRowor XML nodes. Adapters are registered per type in theTypeTable.
Sources: src/System.Management.Automation/engine/TypeTable.cs:2146-2200, src/System.Management.Automation/engine/TypeTable.cs:2486-2500
Conclusion
The Extended Type System (ETS) transforms PowerShell from a simple scripting environment into a powerful object-manipulation engine. By decoupling object data from its presentation and behavior, ETS allows PowerShell to provide a consistent, user-friendly interface for any data type, regardless of its original .NET implementation. Through the use of TypeTable, PSObject, and custom member definitions, PowerShell ensures that objects are always "enhanced" with the metadata necessary for modern automation tasks.