How to use interfaces in Powershell defined via Add-Type?

前端 未结 3 1625
南笙
南笙 2021-01-16 09:22

I developed a PowerShell module that relied on a .NET Assembly for some operations.
I refactored this module to not need that Assembly and noticed some strange behavior,

3条回答
  •  清歌不尽
    2021-01-16 10:14

    To complement your own answer:

    PowerShell class and enum definitions are parsed before execution begins and any types referenced in such definitions must either be:

    • loaded into the current the session beforehand.

    • [only partially implemented as of Windows PowerShell v5.1 / PowerShell Core 6.1.0]
      loaded via a using module / using assembly statement at the very top of the file.

      • using module already works, but only if the referenced types are themselves PowerShell class / enum definitions.

      • Currently, types loaded from assemblies - whether via a module containing assemblies imported with using module or using assembly to directly load an assembly - aren't yet detected.

        • See the following GitHub issues:
          • #3461 (using module) and #2074 (using assembly)
          • #6652 - a meta issue tracking all class-related issues.

    The workaround in the meantime is to load the referenced types via a separate script beforehand, by using the module manifest's NestedModules entry.

    • Note: The ScriptsToProcess entry would work too, but the scripts it references are dot-sourced in (loaded into the current scope of) the caller, not the enclosing module.
      With types from compiled code (including ad hoc-compiled code with Add-Type), that works too, because such types invariably become available session-globally, but it is conceptually cleaner to use NestedModules, because it dot-sources scripts in the enclosing module's scope

提交回复
热议问题