Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7553147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:58:09+00:00 2026-05-30T10:58:09+00:00

Generally, I’m trying to create a PSCmdlet that takes a parameter of a type

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:58:10+00:00Added an answer on May 30, 2026 at 10:58 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Generally, it is good practice to avoid GOTOs. Keeping that in mind I've been
Generally UDP doesn't garantee that packets will arrive in the same order they were
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
Generally speaking, the SQL queries that I write return unformatted data and I leave
Generally don't do OO-programming in Python. This project requires it and am running into
Generally We have some business logic that is causing a bottle neck within a
Generally I have MenuItems model and trying to make '/menu_items(/:id(:/some_action))' URLs looks like '/menu(/:id(:/some_action))'
Generally if there is a property that gets/sets a value for state, I use
Generally I need some online compiler that can compile and execute provided program and
Generally speaking, individual JavaScript files that a page uses should be combined into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.