Powershell - New-Object: 数组索引超出了范围

4

我有一个问题已经几天了,下面这个脚本的执行导致了异常。

我使用多个C#构造函数来帮助输入大量行到数组中。我的问题可能来自于PowerShell语法,代码在C#中可以正常工作。

我对PowerShell很新,我对我正在做的事情没有信心...

$source = @"
using System.IO;

public class DesktopIni
{
    public const string Shell32 = @"%SystemRoot%\system32\shell32.dll";

    // Ctor without folder, with localizedRes
    public DesktopIni(string root,
        int iconResourceIndex, int localizedResourceIndex,
        string iconResource = Shell32, string localizedResourceName = Shell32)
        : this(root, null, iconResourceIndex, iconResource, localizedResourceIndex, localizedResourceName) { }

    // Ctor without folder, without localizedRes
    public DesktopIni(string root,
        int iconResourceIndex, string iconResource = Shell32)
        : this(root, null, iconResourceIndex, iconResource, 0, null) { }



    // Ctor with folder, with localizedRes
    public DesktopIni(string root, string folderName,
        int iconResourceIndex, int localizedResourceIndex,
        string iconResource = Shell32, string localizedResourceName = Shell32)
        : this(root, folderName, iconResourceIndex, iconResource, localizedResourceIndex, localizedResourceName) { }

    // Ctor with folder, without localizedRes
    public DesktopIni(string root, string folderName,
        int iconResourceIndex, string iconResource = Shell32)
        : this(root, folderName, iconResourceIndex, iconResource, 0, null) { }



    // Full Ctor
    private DesktopIni(string root, string folderName,
        int iconResourceIndex, string iconResource,
        int localizedResourceIndex, string localizedResourceName)
    {
        if(!string.IsNullOrEmpty(folderName))
            this.FullPath =  Path.Combine(root, folderName);
        else
            this.FullPath =  root;

        this.IconResource = iconResource;
        this.IconResourceIndex = iconResourceIndex;

        this.LocalizedResourceName = localizedResourceName;
        this.LocalizedResourceIndex = localizedResourceIndex;
    }

    public string FullPath;

    public string IconResource;
    public int IconResourceIndex;

    public string LocalizedResourceName;
    public int LocalizedResourceIndex;
}
"@


Add-Type -TypeDefinition $source


$Drive = $env:HOMEDRIVE + '\'
$Folders = @()

$Folders += New-Object DesktopIni "C:\Demo1", 160
$Folders += New-Object DesktopIni $Drive, "Demo2", 160
$Folders += New-Object DesktopIni "C:\Demo3", 160, -21813
$Folders += New-Object DesktopIni $Drive, "Demo4", 160, -21813

$Folders | Format-Table

异常:

New-Object : Index was outside the bounds of the array.
Au caractère C:\Users\Jeremy\Desktop\2 - Programs\test.ps1:69 : 13
+ $Folders += New-Object DesktopIni $Drive, "Demo2", 160
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-Object], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.PowerShell.Com 
   mands.NewObjectCommand

请告诉我正确的语法,谢谢。

对于另外两行也适用相同的异常处理方法。


这不是数组分配的问题,而是New-Object中使用的参数的问题。在数组分配之外执行New-Object代码,您将看到相同的问题。您需要查看构造函数的编写方式或传递的值。 - ravikanth
1个回答

2

您似乎发现了New-Object的一个错误,以及在定义具有默认参数值的构造函数时它的行为。可以通过一个非常简单的测试类来复现此错误:

Add-Type -TypeDefinition @'
public class TestClass
{
    public TestClass(int anInt, string aString = "") { }
    public TestClass(int anInt) { }
}
'@

New-Object TestClass 1

我建议在Connect网站上报告错误:http://connect.microsoft.com/PowerShell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接