Powershell 无法将my类的实例添加到列表中<;我的班级>;

Powershell 无法将my类的实例添加到列表中<;我的班级>;,powershell,powershell-5.0,Powershell,Powershell 5.0,我有一个Powershell脚本,该脚本声明一个类,然后尝试将该类的实例添加到列表中: Add-Type -TypeDefinition @" using System.Text.RegularExpressions; public class BuildWarning { public string Solution { get; private set; } public string Project { get; private set; } public stri

我有一个Powershell脚本,该脚本声明一个类,然后尝试将该类的实例添加到列表中:

Add-Type -TypeDefinition @"
using System.Text.RegularExpressions;
public class BuildWarning
{
    public string Solution { get; private set; }
    public string Project { get; private set; }
    public string WarningMessage { get; private set; }
    public string WarningCode { get; private set; }
    public string Key { get; private set; }
    public bool IsNew { get; set; }
    private static readonly Regex warningMessageKeyRegex = new Regex(@"^(?<before>.*)\([0-9,]+\)(?<after>: warning .*)$");
    public BuildWarning(string solution, string project, string warningMessage, string warningCode)
    {
        Solution = solution;
        Project = project;
        WarningMessage = warningMessage;
        WarningCode = warningCode;
        var match = warningMessageKeyRegex.Match(WarningMessage);
        Key = Solution + "|" + Project + "|" + match.Groups["before"].Value + match.Groups["after"].Value;
    }
}
"@

[System.Collections.Generic.List``1[BuildWarning]] $warnings = New-Object "System.Collections.Generic.List``1[BuildWarning]"

[BuildWarning] $newWarning = New-Object BuildWarning("", "", "", "")

$warnings += $newWarning
添加类型-TypeDefinition@”
使用System.Text.RegularExpressions;
公共类建筑警告
{
公共字符串解决方案{get;private set;}
公共字符串项目{get;private set;}
公共字符串警告消息{get;private set;}
公共字符串警告代码{get;private set;}
公共字符串密钥{get;private set;}
公共bool是新的{get;set;}
私有静态只读正则表达式warningMessageKeyRegex=新正则表达式(@“^(?.*)\([0-9,]+\)(?:warning.*)$”;
公共构建警告(字符串解决方案、字符串项目、字符串警告消息、字符串警告代码)
{
溶液=溶液;
项目=项目;
警告消息=警告消息;
警告代码=警告代码;
var match=warningMessageKeyRegex.match(WarningMessage);
键=解决方案+“|”+项目+“|”+匹配.Groups[”在“]之前。值+匹配.Groups[”在“]之后。值;
}
}
"@
[System.Collections.Generic.List``1[BuildWarning]]$warnings=新对象“System.Collections.Generic.List``1[BuildWarning]”
[BuildWarning]$newWarning=新对象BuildWarning(“,”,“,”,”)
$warnings+=$newWarning
在最后一行,我得到一个错误:

Cannot convert the "System.Object[]" value of type "System.Object[]" to type "BuildWarning". At C:\development\temp\BuildWarningReportGenerator.ps1:93 char:17 + $warnings += $newWarning + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : ConvertToFinalInvalidCastException 无法将类型为“System.Object[]”的“System.Object[]”值转换为类型 “建筑警告”。 在C:\development\temp\BuildWarningReportGenerator.ps1:93 char:17 +$warnings+=$newWarning + ~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:InvalidArgument:(:)[],运行时异常 +FullyQualifiedErrorId:ConvertToFinalInvalidCastException 我想不出是什么问题。类型检查显示
$warnings
$newwarnings
的类型都是正确的。如何修复此错误?

这样如何

#do not use this way
#$warnings += $newWarning

#but use this instead
$warnings.Add($newWarning)
提供了一个有效的解决方案:

若要将元素附加到您的
[System.Collections.Generic.List`1[BuildWarning]
实例,请使用其
.Add()
方法,而不是PowerShell的
+=
运算符。

PowerShell的
+=
运算符通常将集合值LHS视为一个数组(与特定的LHS集合类型无关)并“附加”到该数组,即创建一个(新)数组,其中包含LHS集合的所有元素,后跟RHS元素

换句话说:使用
+=
忽略特定的LHS集合类型,并始终指定一个(新的)
[object[]]
数组,该数组包含LHS集合的元素和RHS
元素

这种行为可能会令人惊讶,因为期望保留LHS的特定集合类型是合理的-请参阅

在您的特定情况下,从v5.1开始,您在Windows PowerShell中看到一个bug,该bug已在PowerShell Core中修复:

如果在您的案例中尝试键入约束列表变量
$warnings
,则会出现问题。类型约束意味着在LHS变量名之前放置一个类型(强制转换),这将锁定变量的类型,以便后续赋值必须是相同的或兼容的类型

提供一个简单的示例:

$list = New-Object 'System.Collections.Generic.List[int]'
$list += 1  # OK - $list is not type-constrained
Write-Verbose -Verbose "Unconstrained `$list 'extended': $list"


# Type-constrained $list
[System.Collections.Generic.List[int]] $list = New-Object 'System.Collections.Generic.List[int]'
$list += 1  # !! BREAKS, due to the bug
Write-Verbose -Verbose "Type-constrained `$list 'extended': $list"

我鼓励您在中报告此错误。

它确实修复了错误。非常感谢。但是你能解释一下原因吗?