Generally, I’m trying to create a PSCmdlet that takes a parameter of a type that implements IDisposeable and requires disposal in order to avoid leaking resources. I would also like to accept a string for that parameter and create an instance of that type, however if I create that object myself, then I need to dispose it before returning from ProcessRecord.
I’m using an ArgumentTransformationAttribute with my parameter in order to construct my IDisposeable object from a string, but I cannot find any way to pass data from that class to my PSCmdlet about whether I created the object or not. For example:
[Cmdlet("Get", "MyDisposeableName")]
public class GetMyDisposeableNameCommand : PSCmdlet
{
[Parameter(Mandatory = true, Position = 0), MyDisposeableTransformation]
public MyDisposeable MyDisposeable
{
get;
set;
}
protected override void ProcessRecord()
{
try
{
WriteObject(MyDisposeable.Name);
}
finally
{
/* Should only dispose MyDisposeable if we created it... */
MyDisposeable.Dispose();
}
}
}
class MyDisposeableTransformationAttribute : ArgumentTransformationAttribute
{
public override Object Transform(EngineIntrinsics engineIntrinsics, Object input)
{
if (input is PSObject && ((PSObject)input).BaseObject is MyDisposeable)
{
/* We were passed a MyDisposeable, we should not dispose it */
return ((PSObject)input).BaseObject;
}
/* We created a MyDisposeable, we *should* dispose it */
return new MyDisposeable(input.ToString());
}
}
My best guess here is to subclass my MyDisposeableClass just to tag that it needs explicit disposal, but that seems fairly hacky, and while it works in this case, it obviously wouldn’t work if I wanted to deal with a sealed class.
Is there a better way to do this?
In the end, I simply use parameters that accept a type that wraps
MyDisposeable. My initial concern with doing this was that using an internal type for a parameter would impact the functions accessibility. (Perhaps it would negatively impact documentation, but in a cmdlet the documentation is wholly controlled by an XML file.)After some testing, there do not appear to be any problems using an internal class for a parameter and just letting the transformation accept public types. So I simply create a wrapper class:
And have the parameter take that instead. In the transformation attribute, simply set
NeedsDisposedbased on whether the parameter took aMyDisposeableor constructed one. eg: