Class 使用Powershell访问静态类中的静态类

Class 使用Powershell访问静态类中的静态类,class,powershell,static,Class,Powershell,Static,我有一门课,如下所示 namespace Foo.Bar { public static class ParentClass { public const string myValue = "Can get this value"; public static class ChildClass { public const string myChildValue = "I want to get this value";

我有一门课,如下所示

namespace Foo.Bar
{
    public static class ParentClass
    {
      public const string myValue = "Can get this value";

      public static class ChildClass
      {
        public const string myChildValue = "I want to get this value";
      }
     }
}
我可以使用powershell获取myValue

[System.Reflection.Assembly]::LoadWithPartialName("Foo.Bar")
$parentValue = [Foo.Bar.ParentClass]::myValue
但是我无法在myChildValue类中获取该类。有人能帮忙吗

虽然它可能是下面这样,但$childValue始终为空

[System.Reflection.Assembly]::LoadWithPartialName("Foo.Bar")
$childValue = [Foo.Bar.ParentClass.ChildClass]::myChildValue

它是
[Foo.Bar.ParentClass+ChildClass]
。在PowerShell 3上,完成选项卡将告诉您同样的信息。此外,您可以使用
addtype
直接编译和加载代码:

C:\Users\Joey> add-type 'namespace Foo.Bar
>> {
>>     public static class ParentClass
>>     {
>>       public const string myValue = "Can get this value";
>>
>>       public static class ChildClass
>>       {
>>         public const string myChildValue = "I want to get this value";
>>       }
>>      }
>> }'
>>
C:\Users\Joey> [Foo.Bar.ParentClass+ChildClass]::myChildValue
I want to get this value

无需摆弄C#编译器和
[Assembly]::LoadWithPartialName

谢谢你,回答得这么快还需要加分。这就是+符号的含义,所以如果childclass下面有一个类,它会是Foo.Bar.ParentClass+childclass+ChildOfChildClasss
+
是该类的内部名称。C#对名称空间分隔和嵌套类都使用点
,但.NET本身不这样做。当您使用反射来访问类型时,这一点就变得很明显了(在页面的一半,也有文档记录)。是的,嵌套类也会使用
+
。谢谢你的链接和解释。