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

Can*_*der 5 powershell static class

我有一个类如下

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";
      }
     }
}
Run Code Online (Sandbox Code Playgroud)

我可以使用powershell获取myValue,

[System.Reflection.Assembly]::LoadWithPartialName("Foo.Bar")
$parentValue = [Foo.Bar.ParentClass]::myValue
Run Code Online (Sandbox Code Playgroud)

但是我无法在类myChildValue中获得该类.有人可以帮忙吗?

认为它可能类似于下面但$ childValue总是空的.

[System.Reflection.Assembly]::LoadWithPartialName("Foo.Bar")
$childValue = [Foo.Bar.ParentClass.ChildClass]::myChildValue
Run Code Online (Sandbox Code Playgroud)

Joe*_*oey 9

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

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
Run Code Online (Sandbox Code Playgroud)

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

  • `+`来自该类的内部名称.C#对名称空间分离和嵌套类使用点`.`,但.NET本身没有.当您使用反射来访问类型时,这一点就变得很明显了(并且还有文档[http://msdn.microsoft.com/library/w3f99sx1.aspx],大约是页面的一半).所以是的,嵌套的嵌套类也会使用`+`. (2认同)