Compile and load the type... this could be pre-compiled e.g. via dotnet CLI then the type loaded via
Assembly.LoadFrom(...) or Add-Type or Import-Module if part of an actual PowerShell Module.
Once loaded you can Update-TypeData -TypeAdapter
like shown in the example:
Add-Type -Path .\adapterExample.cs -WA 0 -IgnoreWarnings
Update-TypeData -TypeAdapter ([MyPropertyAdapter]) -TypeName ([MyCustomClass])If the adapter is part of the a module you can have a ...Types.ps1xml instead and loaded via TypesToProcess:
<?xml version="1.0" encoding="utf-8"?>
<Types>
<Type>
<Name>MyCustomClass</Name>
<TypeAdapter>
<TypeName>MyPropertyAdapter</TypeName>
</TypeAdapter>
</Type>
</Types>Then the actual usage, pretty much like an AD Object... it is actually ADEntityAdapter the one in charge of this...
$class = [MyCustomClass]::new()
$class.Id = [guid]::NewGuid()
$class.Name = 'john.galt'
$class
# Id Name
# -- ----
# bbd94707-59b8-4de8-9624-89bfa2ca6700 john.galt
$class.psobject.Properties
# BaseObject : MyCustomClass
# Tag : bbd94707-59b8-4de8-9624-89bfa2ca6700
# MemberType : Property
# Value : bbd94707-59b8-4de8-9624-89bfa2ca6700
# IsSettable : True
# IsGettable : True
# TypeNameOfValue : Guid
# Name : Id
# IsInstance : True
#
# BaseObject : MyCustomClass
# Tag : john.galt
# MemberType : Property
# Value : john.galt
# IsSettable : True
# IsGettable : True
# TypeNameOfValue : String
# Name : Name
# IsInstance : True
@mklement0 apparently we can't return
new PSAdaptedProperty(propertyName, customClass[propertyName])always, it has to returnnullwhen it's a method so the adapter falls back to calling it. I added a comment to the code but having a fully dynamic object is clearly not the best idea. Probably the implementer should decide on a list of properties that can be set on the object instead of self reflection like I did there.