【问题标题】:Error when running a dot-sourced script after executing it执行点源脚本后运行时出错
【发布时间】:2021-12-20 14:13:43
【问题描述】:

我有非常奇怪的行为,我认为这与点源有关,但我无法理解它。这是我所拥有的:

包含两个函数和一个类的脚本sourced.ps1:

class MyData {
    [string] $Name
}

function withClass() {
    $initialData = @{
        Name1 = "1";
        Name2 = "2";
    }
    $list = New-Object Collections.Generic.List[MyData]
    foreach ($item in $initialData.Keys) {
        $d = [MyData]::new()
        $d.Name = $item
        $list.Add($d)
    }
}

function withString() {
    $initialData = @{
        Name1 = "1";
        Name2 = "2";
    }
    $list = New-Object Collections.Generic.List[string]
    foreach ($item in $initialData.Keys) {
        $list.Add($item)
    }
}

我还有一个脚本caller.ps1 点源上面的那个并调用函数:

$ErrorActionPreference = 'Stop'

. ".\sourced.ps1"
withClass

然后我通过在 shell(带有 PS Core 的 Win 终端)中执行 .\caller.ps1 来调用 caller.ps1。

这是我无法解释的行为:如果我调用.\caller.ps1,然后调用.\sourced.ps1,然后再次调用caller.ps1,我会收到错误:

Line |
  14 |          $list.Add($d)
     |          ~~~~~~~~~~~~~
     | Cannot find an overload for "Add" and the argument count: "1".

但是,如果我将caller.ps1 改为调用withString 函数,那么无论我调用多少次caller.ps1 和sourced.ps1,一切正常。

此外,如果我先用withString 调用caller.ps1,然后将其更改为withClass,则不会出现任何错误。

我想使用模块会更正确,但我首先对这种奇怪行为的原因感兴趣。

【问题讨论】:

  • 与问题无关,但为什么不,因为您已经在创建一个类,创建可以替换您的类的外部函数的方法?
  • 这是一个主观的事情,我个人更喜欢在可能的情况下分离数据和行为并使用纯函数,因为这是在我倾向于喜欢的语言中完成的。如果我不需要访问并且不经常改变实例,我不使用方法而是使用函数。

标签: powershell powershell-core


【解决方案1】:

从 PowerShell 7.2.1 开始编写

  • 一个既定的脚本文件,它是点源和直接执行的(以任意顺序,不管频率如何)在其中创建class 定义的连续版本 - 这些是不同的 .NET 类型,即使它们的结构相同。可以说,没有充分的理由这样做,并且该行为可能是一个错误。

  • 这些版本,全名相同(在顶级脚本范围内创建的 PowerShell class 定义只有一个名称,没有命名空间),但位于不同的动态(内存中)程序集中,它们的版本号的最后一个组件不同,shadow 彼此不同,效果取决于哪个上下文:

    • 点源此类脚本的其他脚本始终会看到 新 版本。
    • 在脚本本身内部,无论它本身是直接执行还是点源执行:
      • 在 PowerShell 代码中,原始版本仍然有效。
      • 在 binary cmdlet 中,特别是 New-Object,新 版本生效。
      • 如果您混合这两种方式来访问脚本内的类,类型不匹配可能会发生,这就是您的情况 - 请参阅下面的示例代码。
  • 虽然您可以通过始终使用::new() 或 New-Object 来引用类,在技术上避免此类错误,但最好避免直接执行这两个包含class 定义的脚本文件的执行和点源。

示例代码:

  • 将代码保存到脚本文件,例如demo.ps1

  • 执行它两次。

    • 首先,通过直接执行:.\demo.ps1
    • 然后,通过dot-sourcing:. .\demo.ps1
  • 您看到的类型不匹配错误将在第二次执行期间发生。

    • 注意:错误消息Cannot find an overload for "Add" and the argument count: "1" 有点模糊;它试图表达的是 .Add() 方法不能用给定类型的参数 调用,因为它需要一个 new 版本的 @ 实例987654333@,而::new() 创建了原始版本的实例。
# demo.ps1

# Define a class 
class MyData { }

# Use New-Object to instantiate a generic list based on that class.
# This passes the type name as a *string*, and instantiation of the 
# type happens *inside the cmdlet*.
# On the second execution, this will use the *new* [MyData] version.
Write-Verbose -Verbose 'Constructing list via New-Object'
$list = New-Object System.Collections.Generic.List[MyData]

# Use ::new() to create an instance of [MyData]
# Even on the second execution this will use the *original* [MyData] version
$myDataInstance = [MyData]::new()

# Try to add the instance to the list.
# On the second execution this will *fail*, because the [MyData] used
# by the list and the one that $myDataInstance is an instance of differ.
$list.Add($myDataInstance)

请注意,如果您使用$myDataInstance = New-Object MyData,类型不匹配将会消失。

同样,如果您坚持使用 ::new() 并使用它来实例化 列表,它也会消失:$list = [Collections.Generic.List[MyData]]::new()

【讨论】:

    猜你喜欢
    • 2015-01-31
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2021-03-28
    相关资源
    最近更新 更多