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
Thanks for updating - works great.
In general, it's worth noting that property adapters are primarily designed for adapting preexisting, PowerShell-unaware types.
If, as in the case at hand, the property adapter must rely on
internalmembers of the adapted type anyway, you could simplify the implementation by limiting it to simply exposing the dictionary storing the dynamic properties, and making the adapter do the rest.On an unrelated note, re https://stackoverflow.com/q/79939851/45375: