2014-06-15 2 views
2

며칠 동안 문제가 발생했습니다. 아래 스크립트를 실행하면 실행이 중지됩니다.Powershell - New-Object : 인덱스가 배열의 경계를 벗어났습니다.

여러 C# 생성자를 사용하여 배열의 많은 수의 행을 입력하는 데 도움이됩니다. 내 문제는 아마도 내 PowerShell 구문에서 코드가 C#에서 올바르게 작동합니다. 내가 파워 쉘에 새로운 해요

, 내가 ... 내가 뭘하는지에 대해 확신하지 않다

$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 행에 대해 동일한 예외 .

정확한 구문을 알려주시겠습니까? 감사합니다.

+0

이 배열의 할당에 문제가되지 않습니다. New-Object에서 사용되는 매개 변수에 문제가 있습니다. 배열 할당 밖에서 New-Object 코드를 실행하면 동일한 문제가 나타납니다. 생성자가 작성되는 방법이나 전달할 값을 확인해야합니다. – ravikanth

답변

0

New-Object에서 버그를 발견 한 것으로 보이며 기본 매개 변수 값을 가진 생성자가 정의되어있을 때 어떻게 동작하는지 알 수 있습니다. 이 오류는 매우 간단한 테스트 클래스를 재현 할 수 있습니다

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

New-Object TestClass 1 

나는 연결 사이트에 버그를 통해보고 좋을 것 : http://connect.microsoft.com/PowerShell

관련 문제