Relation between IsAssembly / IsFamily and IsFamilyOrAssembly

399 views Asked by At

IsAssembly, IsPublic, IsFamily, IsFamilyOrAssembly, IsFamilyAndAssembly I have read about this but I am not able to understand what each one does. The strange thing here is IsFamily and IsAssembly returns False in the code but IsFamilyOrAssembly returns True.

Can someone give an explanation for each of this property as I find it difficult to understand from the documentation.I came across all this when I started reading about Reflection in 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
                );
            }
        }
    }
}

This code example produces output similar to the following:

                              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

There are 2 answers

0
Ha Hoang On BEST ANSWER

Member of a Class have access modifiers associated with them (public, internal,...). These define the level of object-oriented encapsulation implemented by the member. You can find more details at here.

Using Reflection, you might want to have a look at:

                    /*Modifiers*/

IsPublic            public

IsFamilyOrAssembly  protected internal

IsFamily            protected

IsFamilyAndAssembly private protected (since C# 7.2)

IsAssembly          internal

When you want to determine if a member is visible in inherited types, you need to check using the expression (m.IsFamilyOrAssembly || m.IsFamily || m.IsFamilyAndAssembly || m.IsAssembly). And exactly only one of these properties is true and all of the others are false.

0
Damien_The_Unbeliever On

These methods check for certain enumeration values from the 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
    ...

We can observe that FamORAssem and FamANDAssem are distinct values, and they are not related to the Family or Assembly values. Confusingly, this enum is marked with the Flags attribute, but when it comes to the member access values, they're not simple bitwise combinations.