I'm trying to create a PowerShell-class implementation of an abstract class' function:
class ReadList : Intacct.SDK.Functions.AbstractFunction {
[string]$ObjectName
ReadList ([string]$ObjectName) {
$this.ObjectName = $ObjectName
}
[void] WriteXml ( [Intacct.SDK.Xml.IaXmlWriter]$xml ) {
$xml.WriteStartDocument()
$xml.WriteStartElement("get_list")
$xml.WriteAttributeString("object",$this.ObjectName)
$xml.WriteEndElement() # </get_list>
$xml.WriteEndDocument()
$xml.Flush()
$xml.Close()
}
}
When I attempt to use it, I get a run-time exception:
Error during creation of type "ReadList". Error message: Method 'WriteXml' in type 'ReadList' from assembly 'PowerShell Class Assembly, Version=1.0.0.1, | Culture=neutral, PublicKeyToken=null' does not have an implementation.
I've tried adding the ref
tag:
[void] WriteXml ( [Intacct.SDK.Xml.IaXmlWriter][ref]$xml ) {
But I get a different error:
Multiple type constraints are not allowed on a method parameter.
Am I doing something wrong, or is what I'm trying to do not supported?
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 theMethod '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 inc#
, and adding to PowerShell viaAdd-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 theMultipleTypeConstraintsOnMethodParam
exception that gets raised.Scripting.Classes.BasicParsing.Tests.ps1
checks multiple types will raise aMultipleTypeConstraintsOnMethodParam
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
solutionReferencedAssemblies
to pass inIntacct.SDK.dll
and dependent assemblies.[Reflection.Assembly]
Use
Load()
when specifying version orLoadFromPartialName()
with just the name,LoadFrom()
when specifying the path.