Question

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?

Was it helpful?

Solution 2

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:

public class MyDisposeableWrapper
{
    public MyDisposeable MyDisposeable
    {
        get;
        set;
    }

    public bool NeedsDisposed
    {
        get;
        set;
    }

    public MyDisposeableWrapper(MyDisposeable myDisposeable, bool needsDisposed)
    {
        MyDisposeable = myDisposeable;
        NeedsDisposed = needsDisposed;
    }
}

And have the parameter take that instead. In the transformation attribute, simply set NeedsDisposed based on whether the parameter took a MyDisposeable or constructed one. eg:

if(input is MyDisposeable)
{
    return new MyDisposeableWrapper((MyDisposeable) input, false);
}
else
{
    /* construct MyDisposeable from the input */
    return new MyDisposeableWrapper(myDisposeable, true);
}

OTHER TIPS

Rather than subclassing, can you add a property to your MyDisposable class?

public class MyDisposable
{
    ...   
    public bool IsAttributeCreated { get; set; }
}

Then in your attribute code

/* We created a MyDisposeable, we *should* dispose it */
return new MyDisposeable(input.ToString()){IsAttributeCreated=true};

Finally in your finally block

finally
{
    /* Should only dispose MyDisposeable if we created it... */
    if (MyDisposable.IsAttributeCreated)
        MyDisposeable.Dispose();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top