IsAssembly / IsFamily 和 IsFamilyOrAssembly 之间的关系

Relation between IsAssembly / IsFamily and IsFamilyOrAssembly

IsAssembly, IsPublic, IsFamily, IsFamilyOrAssembly, IsFamilyAndAssembly 我读过这个,但我无法理解每个人的作用。这里奇怪的是代码中的IsFamilyIsAssemblyreturnsFalse但是IsFamilyOrAssembly returns True.

有人可以对每个 属性 进行解释吗,因为我发现很难理解 documentation.I 当我开始阅读有关 C# 中的反射时遇到的所有这些。

public class Example
{
    public void m_public() {}
    internal void m_internal() {}
    protected void m_protected() {}
    protected internal void m_protected_public() {}

    public static void Main()
    {
        Console.WriteLine("\n{0,-30}{1,-18}{2}", "", "IsAssembly", "IsFamilyOrAssembly"); 
        Console.WriteLine("{0,-21}{1,-18}{2,-18}{3}\n", 
            "", "IsPublic", "IsFamily", "IsFamilyAndAssembly");

        foreach (MethodBase m in typeof(Example).GetMethods(
            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
        {
            if (m.Name.Substring(0, 1) == "m")
            {
                Console.WriteLine("{0,-21}{1,-9}{2,-9}{3,-9}{4,-9}{5,-9}", 
                    m.Name,
                    m.IsPublic,
                    m.IsAssembly,
                    m.IsFamily,
                    m.IsFamilyOrAssembly,
                    m.IsFamilyAndAssembly
                );
            }
        }
    }
}

此代码示例产生类似于以下内容的输出:

                              IsAssembly        IsFamilyOrAssembly
                     IsPublic          IsFamily          IsFamilyAndAssembly

m_public             True     False    False    False    False
m_internal           False    True     False    False    False
m_protected          False    False    True     False    False
m_protected_public   False    False    False    True     False

Class 的成员具有与其关联的访问修饰符(public、内部、...)。这些定义了成员实现的面向对象的封装级别。您可以在 here.

找到更多详细信息

使用反射,您可能想看看:

                    /*Modifiers*/

IsPublic            public

IsFamilyOrAssembly  protected internal

IsFamily            protected

IsFamilyAndAssembly private protected (since C# 7.2)

IsAssembly          internal

当您想要确定一个成员在继承类型中是否可见时,您需要使用表达式 (m.IsFamilyOrAssembly || m.IsFamily || m.IsFamilyAndAssembly || m.IsAssembly)。这些属性中只有一个是 true,其他所有属性都是 false.

这些方法检查 MethodAttributes enumeration. Unfortunately, this is a rather complex enum type. If we examine it's early values:

中的某些枚举值
public enum MethodAttributes
{
    // NOTE: This Enum matchs the CorMethodAttr defined in CorHdr.h

    // member access mask - Use this mask to retrieve accessibility information.
    MemberAccessMask    =   0x0007,
    PrivateScope        =   0x0000,     // Member not referenceable.
    Private             =   0x0001,     // Accessible only by the parent type.  
    FamANDAssem         =   0x0002,     // Accessible by sub-types only in this Assembly.
    Assembly            =   0x0003,     // Accessibly by anyone in the Assembly.
    Family              =   0x0004,     // Accessible only by type and sub-types.    
    FamORAssem          =   0x0005,     // Accessibly by sub-types anywhere, plus anyone in assembly.
    Public              =   0x0006,     // Accessibly by anyone who has visibility to this scope.    
    // end member access mask
    ...

我们可以观察到 FamORAssemFamANDAssem 是不同的值,它们与 FamilyAssembly 值无关。令人困惑的是,这个枚举标有 Flags 属性,但是当涉及到成员访问值时,它们并不是简单的按位组合。