IsAssembly / IsFamily与IsFamilyOrAssembly之间的关系

时间:2017-01-06 05:39:36

标签: c# .net .net-assembly assemblies system.reflection

IsAssembly, IsPublic, IsFamily, IsFamilyOrAssembly, IsFamilyAndAssembly 我已经读过这个,但我无法理解每个人的所作所为。这里奇怪的是 IsFamily IsAssembly 在代码中返回False IsFamilyOrAssembly 返回True

有人可以为每个属性提供解释,因为我发现很难从文档中理解。当我开始阅读c#中的Reflection时,我遇到了所有这些。

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

2 个答案:

答案 0 :(得分:3)

Class的成员具有与之关联的访问修饰符(public,internal,...)。这些定义了成员实现的面向对象封装的级别。您可以在here找到更多详细信息。

使用Reflection,您可能需要查看:

                    /*Modifiers*/

IsPublic            public

IsFamilyOrAssembly  protected internal

IsFamily            protected

IsFamilyAndAssembly n/a

IsAssembly          internal

如果要确定成员在继承类型中是否可见,则需要使用表达式进行检查( m.IsFamilyOrAssembly || m.IsFamily || < strong> m.IsFamilyAndAssembly || m.IsAssembly )。而且这些属性中只有一个是 true ,而其他所有属性都是 false

答案 1 :(得分:0)

这些方法检查RFC 7622 § 3.2枚举中的某些枚举值。不幸的是,这是一个相当复杂的枚举类型。如果我们检查它是MethodAttributes

.clearfix:before,
.clearfix:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.clearfix:after {
    clear: both;
}

我们可以观察到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 ... FamORAssem是不同的值,并且它们与FamANDAssemFamily值无关。令人困惑的是,这个枚举用Assembly属性标记,但是当涉及成员访问值时,它们不是简单的按位组合。