c# - Powershell - 新对象 : Index was outside the bounds of the array

标签 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 行也有相同的异常。

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

最佳答案

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

关于c# - Powershell - 新对象 : Index was outside the bounds of the array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24234417/

相关文章:

image - 在 PowerShell 中提取 EXIF 数据的简便方法?

powershell - Lync - PowerShell - 用户状态

powershell - 将文件向上移动一个文件夹级别

c# - 如何在 Entity Framework 中使用 join 使输出 Json 对象分级别 - 不是同一级别

c# - 如何从 WCF 服务返回图像?

c# - 'Class' 是一个 'type' 但像 'variable' 一样使用

powershell - 获取远程设备的 Chrome 版本

c# - 无法加载文件或程序集或其依赖项之一。该系统找不到指定的文件。 (不允许 GAC)

c# - WCF 服务引用生成自己的契约接口(interface),不会重用我的

powershell - 根据文本文件 PowerShell 中的时间戳删除行