Powershell 使用New Object-Property设置嵌套类的属性

Powershell 使用New Object-Property设置嵌套类的属性,powershell,powershell-2.0,Powershell,Powershell 2.0,假设我在powershell脚本中用C#定义以下类: Add-Type -TypeDefinition @" public class InnerClass { int a, b; public int A { get { return a; } set { a = value; } } public int B { get { return b; } set { b = value; } } } public class SomeClass { string n

假设我在powershell脚本中用C#定义以下类:

Add-Type -TypeDefinition @"
public class InnerClass {
    int a, b;
    public int A { get { return a; } set { a = value; } }
    public int B { get { return b; } set { b = value; } }
}
public class SomeClass {
    string name;
    public string Name { get { return name; } set { name= value; } }
    InnerClass numbers;
    public InnerClass Numbers { get { return numbers; } set { numbers = value; } }
}
"@
我可以实例化
InnerClass
的一个实例,如下所示:

New-Object InnerClass -Property  @{
    'A' = 1;
    'B' = 2;
}
但是,如果我想实例化
SomeClass
,并以类似的方式设置
InnerClass
的属性,它就会失败

New-Object SomeClass -Property @{
    'Name' = "Justin Dearing";
    Numbers = @{
        'A' = 1;
        'B' = 2;
    };
} ;

New-Object : The value supplied is not valid, or the property is read-only. Cha
nge the value, and then try again.
At line:20 char:11
+ New-Object <<<<  SomeClass -Property @{
    + CategoryInfo          : InvalidData: (:) [New-Object], Exception
    + FullyQualifiedErrorId : InvalidValue,Microsoft.PowerShell.Commands.NewOb 
   jectCommand



Name    : Justin Dearing
Numbers : 
新对象SomeClass-属性@{
'Name'=“贾斯汀·迪林”;
数字=@{
‘A’=1;
‘B’=2;
};
} ;
新对象:提供的值无效,或属性为只读。恰恰
请重新设置该值,然后重试。
第20行字符:11

+新对象你应该这样做我相信:

New-Object SomeClass -Property @{
     'Name' = "Justin Dearing";
     Numbers = New-Object InnerClass -Property  @{
                'A' = 1;
                'B' = 2;
            };
} ;

不能直接使用,但可以使用内联方式构造innerclass

New-Object SomeClass -Property @{
'Name' = "Justin Dearing";
'Numbers' = New-Object InnerClass -Property  @{
  'A' = 1;
  'B' = 2;
 }  
};