为什么可以从属性访问私有const字段?

Vah*_*ahe 6 c# oop attributes encapsulation private-members

这怎么可能?

namespace test
    {
        class Attr:Attribute
        {
            public Attr(int e)
            {
            }
        }

        [Attr(E)]
        class Test
        {
            private const int E = 0;
        }
    }
Run Code Online (Sandbox Code Playgroud)

它不违反封装原则吗?

Jer*_*ert 3

不,这并不违反封装。属性声明在逻辑上是类的一部分。Attr没有访问Test.E(它不能),您正在从 内部调用Attrwith的构造函数。这就像初始化一个成员一样好。ETest

C# 语法可能会使该属性看起来像是在类“外部”,但事实并非如此。为该类生成的 IL 是这样的:

.class private auto ansi beforefieldinit test.Test
    extends [mscorlib]System.Object
{
    .custom instance void test.Attr::.ctor(int32) = (
        01 00 00 00 00 00 00 00
    )
    // Fields
    .field private static literal int32 E = int32(0)

    ...

} // end of class test.Test
Run Code Online (Sandbox Code Playgroud)

如果 C# 采用类似的语法,它可能看起来像这样:

    class Test
    {
        attribute Attr(E);

        private const int E = 0;
    }
Run Code Online (Sandbox Code Playgroud)

这会强调声明的范围,但可能不会那么明确。当属性应用于成员时就变得更加不清楚(在 IL 中,这些属性直接位于声明之后)。