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

时间:2014-06-15 21:50:06

标签: c# powershell

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

我使用多个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行相同的例外。

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

1 个答案:

答案 0 :(得分: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