As far as I can tell this is not supported. I have not been able to find a good workaround, or a resource that succinctly explains it.
Technically the second implementation is correct, but as per the error PowerShell does not support multiple type constraints on a method parameter[1].
Normally this isn't the end of the world. The problem is when inheriting from an abstract class, the class must define all of the abstract methods [2], so a method with signature WriteXml (ref IaXmlWriter <ParameterName>)
must be defined. Anything else gives the Method 'WriteXml' ... does not have an implementation.
Even if PowerShell supported multiple type constraints, I don't think this would work as intended because the c#
is not the same as PowerShell's [ref]
- [ref]
was created to support COM Objects and the documentation explicitly states it can't be used to type-cast class members[3].
The only workaround I'm aware of that might work here is writing the ReadList
class definition in c#
, and adding to PowerShell via Add-Type
[4]... not great.
All this is beyond the scope of my knowledge; maybe there is a good solution and someone with more know-how can correct me.
References
[1] SO post that touches on this and [ref] (same as below)
Looking at $Error.Exception.StackTrace
, System.Management.Automation.ScriptBlock.Create
is raising the exception. Couldn't go further with PowerShell 5.1 but PowerShell Core files include a check for class method parameters here which points to the MultipleTypeConstraintsOnMethodParam
exception that gets raised.
Scripting.Classes.BasicParsing.Tests.ps1
checks multiple types will raise a MultipleTypeConstraintsOnMethodParam
exception.
[2] Derived classes of the abstract class must implement all abstract methods.
Abstract and Sealed Classes and Class Members
[3] SO post that touches on this and multiple type constraints (same as above)
The PSReference type is not supported with class members
about_Classes
[4] Add a .NET type to a session
Edit
To elaborate on the Add-Type
solution
- Use
ReferencedAssemblies
to pass in Intacct.SDK.dll
and dependent assemblies.
- Load these assemblies using
[Reflection.Assembly]
Use Load()
when specifying version or LoadFromPartialName()
with just the name, LoadFrom()
when specifying the path.
$Assemblies = @(
'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
'System.Xml.ReaderWriter, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
) |
foreach {
[Reflection.Assembly]::Load($_)
}
$Assemblies += @(
"$pwd/Microsoft.Extensions.Logging.Abstractions.dll"
"$pwd/Intacct.SDK.dll"
) | foreach {
[Reflection.Assembly]::LoadFrom($_)
}
Add-Type -TypeDefinition $Source -ReferencedAssemblies $Assemblies