简体   繁体   English

如何访问另一个静态类内部的静态类的成员

[英]How to access member of a static class which is inside another static class

I have following class structure 我有以下课程结构

public class MainClass
{
    private static class Class1
    {
         private static class Class2
         {
             public const int Id = 2;
         }
    }

    public void getId()
    {
        // I want to access Id here
    }
}

Now I want to access the variable Id which is inside Class2 现在我要访问Class2内的变量Id

I tried like Class1.Class2.Id; 我试过像Class1.Class2.Id; But it is not working 但这不起作用
What I am doing wrong? 我做错了什么?

If you want to access this from outside of the Class1 you need to change the access modifier from private to public (accessible from anywhere) or internal (accessible from the assembly). 如果要从Class1外部访问此文件,则需要将访问修饰符从private更改为public (可从任何地方访问)或internal (可从程序集访问)。

public class MainClass
{
    private static class Class1
    {
         // note the modifier change for Class2
         public static class Class2
         {
             public const int Id = 2;
         }
    }

    public void getId()
    {
        var id = Class1.Class2.Id;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM