Skip to content

New-Object doesn't work with classes in nested modules #2963

Closed

Description

@juneb

Steps to reproduce

  1. Define some PowerShell classes in a module file (ClassTester.psm1).
  2. In the manifest, leave RootModule empty and place the module name in the NestedModules key value.
ModuleToProcess = ''
NestedModules = 'ClassTester.psm1'
  1. In a script, use a Using module statement to import the module and its classes, use the New static method to instantiate one of the classes defined in ClassTester.psm1, and then use New-Object on the same class.
using module ClassTester
$myDog = [Dog]::New('Fido')
$myDog.Bark()

New-Object -TypeName Dog -ArgumentList 'Fluffy'
$myDog.Bark()

Expected behavior

Both the New method and New-Object cmdlet should work.

Actual behavior

New works, but New-Object generates an error.

'Woof!,' said Fido  # New method works
New-Object : Cannot find type [Dog]: verify that the assembly containing this type is loaded.
At C:\ps-test\Test-ClassTester.ps1:22 char:11
+ $fluffy = New-Object -TypeName Dog -ArgumentList 'Fluffy'
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

Environment data

Name                           Value
----                           -----
GitCommitId                    v6.0.0-alpha.14
PSRemotingProtocolVersion      2.3
BuildVersion                   3.0.0.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSVersion                      6.0.0-alpha
PSEdition                      Core
CLRVersion

Activity

  1. xavylon commented on Jun 14, 2017

    @xavylon

    Solution: The issue is that you must call the constructor via a method call when you invoke the New-Object cmdlet. See the attached example code.

    # A constuctor is a method. It must be called as such.
    # you cannot use a argument list for this.
    
    $dog = New-Object Dog("Fido")
    $dog.bark()
  2. mklement0 commented on Apr 14, 2018

    @mklement0
    Contributor

    xavylon:

    There is no syntax issue here:

    New-Object -TypeName Dog -ArgumentList 'Fido' is the proper (if verbose) way to call a constructor via New-Object; its concise equivalent is New-Object Dog Fido (yes, the quotes are optional here).

    In your example, Dog("Fido") is not a method call; it's a syntax based on a misconception that's unfortunately very common and persists because it just so happens to work:

    New-Object is a command and therefore accepts arguments in argument mode, not expression mode (you can only perform method calls in the latter).

    Dog and ("Fido") are therefore separate positional arguments, which bind to -TypeName and -ArgumentList, respectively.

  3. mklement0 commented on Apr 14, 2018

    @mklement0
    Contributor

    June Blender (@juneb):

    From what I can tell - at least as of PowerShell Core v6.0.2 - even the static ::new() method doesn't see classes from nested modules.

    It looks like the crux of the issue is that nested modules are imported with the (equivalent of) Import-Module rather than using, rendering any of the nested module's classes inaccessible - whether via ::new() or via New-Object.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions