【问题标题】:Powershell - New-Object : Index was outside the bounds of the arrayPowershell - 新对象:索引超出了数组的范围
【发布时间】:2014-06-15 21:50:06
【问题描述】:

我这几天有问题,执行下面的脚本会导致执行。

我使用多个 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

其他 2 行也有同样的例外。

你能告诉我正确的语法吗,谢谢。

【问题讨论】:

  • 这不是数组分配的问题。这是New-Object中使用的参数的问题。在数组赋值之外执行 New-Object 代码,你会看到同样的问题。您需要查看构造函数的编写方式或您传递的值。

标签: c# powershell


【解决方案1】:

您似乎发现了 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 2015-04-19
    • 1970-01-01
    • 2015-11-06
    • 2010-11-17
    相关资源
    最近更新 更多