V2 CTP3: Is there a way to undo the action of Add-Type?


  1.    29 Apr 2009 #1
    Chuck Heatherly Guest

    V2 CTP3: Is there a way to undo the action of Add-Type?


    I have this PowerShell script, Abc.ps1:

    $source = @"
    public class Abc
    {
    public void One()
    {
    System.Console.WriteLine("Abc");
    }
    }
    "@

    Add-Type -TypeDefinition $source

    When I run this from a PowerShell prompt, it's fine. If I edit the
    script and modify it such that the compiled code is different (say
    change the "Abc" string to "Def"), then when I try to run the script
    in the same prompt window, I get this error:

    Add-Type : Cannot add type. The type name 'Abc' already exists.
    At C:\Public\Abc.ps1:11 char:9
    + Add-Type <<<< -TypeDefinition $source
    + CategoryInfo : InvalidOperation: (Abc:String)
    [Add-Type], Exception
    + FullyQualifiedErrorId :
    TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand

    I don't want to have to keep opening a new prompt window everytime I
    change the source, so is there a way I can remove this type, or
    otherwise keep this error from happening?

    Thanks,
    Chuck
      My System SpecsSystem Spec

  2.    29 Apr 2009 #2
    Alex K. Angelopoulos Guest

    Re: V2 CTP3: Is there a way to undo the action of Add-Type?


    The problem you're encountering is that once you load an assembly into a
    ..NET application, the types it contains are not released until the
    application shuts down. There are two workarounds that can allow you to do
    something that looks like this, but nothing that is truly equivalent to
    erasing the type and starting over.

    One technique is to modify the name each time you try to use it - awkward
    and not pretty. The other technique is to run the code within a new
    pssession each time you change it. I haven't tried this yet since I don't
    have WinRM configured properly for local loopback, but I believe it should
    create an isolated local pssession that you can try things with and then
    release when un-needed.


    "Chuck Heatherly" <chuck.heatherly@xxxxxx> wrote in message
    news:k7pgv4h4t6a7hjqaue2k13g1sdbe2f3bnq@xxxxxx

    > I have this PowerShell script, Abc.ps1:
    >
    > $source = @"
    > public class Abc
    > {
    > public void One()
    > {
    > System.Console.WriteLine("Abc");
    > }
    > }
    > "@
    >
    > Add-Type -TypeDefinition $source
    >
    > When I run this from a PowerShell prompt, it's fine. If I edit the
    > script and modify it such that the compiled code is different (say
    > change the "Abc" string to "Def"), then when I try to run the script
    > in the same prompt window, I get this error:
    >
    > Add-Type : Cannot add type. The type name 'Abc' already exists.
    > At C:\Public\Abc.ps1:11 char:9
    > + Add-Type <<<< -TypeDefinition $source
    > + CategoryInfo : InvalidOperation: (Abc:String)
    > [Add-Type], Exception
    > + FullyQualifiedErrorId :
    > TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand
    >
    > I don't want to have to keep opening a new prompt window everytime I
    > change the source, so is there a way I can remove this type, or
    > otherwise keep this error from happening?
    >
    > Thanks,
    > Chuck
      My System SpecsSystem Spec

  3.    01 May 2009 #3
    Josh Einstein Guest

    Re: V2 CTP3: Is there a way to undo the action of Add-Type?


    As Alex stated, no you can't do that but why are you using C# as an
    interactive scripting environment within PowerShell? You could grab
    something like Snippet Compiler or LINQPad to do that and it'll be far
    better suited at experimenting with C# blocks than doing it with PowerShell
    which is awkward at best.

    I presume Add-Type is mostly intended to be an alternative to bundling
    pre-compiled DLL's.

    "Chuck Heatherly" <chuck.heatherly@xxxxxx> wrote in message
    news:k7pgv4h4t6a7hjqaue2k13g1sdbe2f3bnq@xxxxxx

    > I have this PowerShell script, Abc.ps1:
    >
    > $source = @"
    > public class Abc
    > {
    > public void One()
    > {
    > System.Console.WriteLine("Abc");
    > }
    > }
    > "@
    >
    > Add-Type -TypeDefinition $source
    >
    > When I run this from a PowerShell prompt, it's fine. If I edit the
    > script and modify it such that the compiled code is different (say
    > change the "Abc" string to "Def"), then when I try to run the script
    > in the same prompt window, I get this error:
    >
    > Add-Type : Cannot add type. The type name 'Abc' already exists.
    > At C:\Public\Abc.ps1:11 char:9
    > + Add-Type <<<< -TypeDefinition $source
    > + CategoryInfo : InvalidOperation: (Abc:String)
    > [Add-Type], Exception
    > + FullyQualifiedErrorId :
    > TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand
    >
    > I don't want to have to keep opening a new prompt window everytime I
    > change the source, so is there a way I can remove this type, or
    > otherwise keep this error from happening?
    >
    > Thanks,
    > Chuck
      My System SpecsSystem Spec

V2 CTP3: Is there a way to undo the action of Add-Type?

Similar Threads
Thread Forum
CTP3: ISE
I hope to see these features for the next release:- 01) a wordwrap functionality (in Script Pane) 02) a shortcut key to run script (in Command...
PowerShell
CTP3 get-help problems
Hi, I cannot seem to get more than the summary with the get-help commandlet ? get-help get-service get-help get-service -full both only...
PowerShell
[CTP3] Help - if you are not using en-US locale
When you are using systems not using en-US locale, ISE's help-menue entry showes nothing and get-help about-* finds nothing. For a german...
PowerShell
[ctp3] Needs ISE .NET Framework 3.5.1
Hello, I'm testing V2 CTP 3 in Window Vista Virtual Box. For unknown reasons .NetFramework 3.5.1 setup has failed repeatedly. In Download...
PowerShell
Display actual file type, not description of the type
Is it possible, in Windows Explorer Search, to display the actual type, rather than the description of the type? eg .mp3 rather than "MPEG...
Vista file management
Display the actual type, rather than the description of the type
Is it possible, in Windows Explorer Search, to display the actual type, rather than the description of the type? eg .mp3 rather than "MPEG...
Vista General
Unable to find type [Drawing.Image]: make sure that the assembly containing this type is loaded
what must i always do to avoid this common error : make sure that the assembly containing this type is loaded ?
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 06:56.
    .