首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell - New-Object :索引超出了数组的界限

Powershell - New-Object :索引超出了数组的界限
EN

Stack Overflow用户
提问于 2014-06-16 05:50:06
回答 1查看 2.1K关注 0票数 4

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

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

我是Powershell的新手,我对我正在做的事情没有信心...

代码语言:javascript
复制
$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

例外:

代码语言:javascript
复制
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行也有同样的例外。

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

EN

回答 1

Stack Overflow用户

发布于 2014-06-30 07:29:53

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

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24234417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档