Skip to content

Add-Type now quietly ignores attempts to change a previously added .NET type's definition in the same session #9575

@mklement0

Description

@mklement0

This is a regression from Windows PowerShell, where the - useful - behavior is as follows:

  • If you repeatedly call Add-Type -TypeDefinition/-MemberDefinition for the same target type, only the first invocation adds the type to the session and subsequent calls are benign - and fast - no-ops, if the source code is unchanged.

  • If you modify the source code, you get an error along the lines of:
    Add-Type : Cannot add type. The type name '<name>' already exists.

This behavior is sensible, as it alerts you to the fact that trying to change the type in-session isn't possible.

By contrast, PowerShell Core now quietly ignores attempts to change the type, which means that you may not notice that your changes didn't take effect.

Steps to reproduce

Run the following Pester test

{
  Add-Type -TypeDefinition @'
  public class Foo {}
'@
  
  # Try to redefine the type with different source code.
  Add-Type -TypeDefinition @'
  public class Foo { public int Bar {get {return 42;} } }
'@
  
} | Should -Throw

Expected behavior

The test should succeed, because an error should be emitted.

Actual behavior

The test fails, because no error is emitted, because the second Add-Type is effectively quietly ignored [a new assembly is actually being created - see
@daxian-dbw's comment below]

Environment data

PowerShell Core v6.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions