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

前端 未结 3 1614
南笙
南笙 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:15

    Apparently PowerShell will read the file and handle class-definitions before executing the code.

    To solve this problem, the Add-Type needs to be put into an own script file, which is run before the module loads (or in ISE, just run the code before running the class definitions).

    This can be accomplished by using ScriptsToProcess from the PSD1 file.

    Kudos to @TheIncorrigible1 for putting me on the track.

提交回复
热议问题