What does the [Flags] Enum Attribute mean in C#?

前端 未结 13 2308
慢半拍i
慢半拍i 2020-11-21 04:21

From time to time I see an enum like the following:

[Flags]
public enum Options 
{
    None    = 0,
    Option1 = 1,
    Option2 = 2,
    Option3 = 4,
    Op         


        
13条回答
  •  青春惊慌失措
    2020-11-21 04:50

    Apologies if someone already noticed this scenario. A perfect example of flags we can see in reflection. Yes Binding Flags ENUM.

    [System.Flags]
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Serializable]
    public enum BindingFlags
    

    Usage

    // BindingFlags.InvokeMethod
    // Call a static method.
    Type t = typeof (TestClass);
    
    Console.WriteLine();
    Console.WriteLine("Invoking a static method.");
    Console.WriteLine("-------------------------");
    t.InvokeMember ("SayHello", BindingFlags.InvokeMethod | BindingFlags.Public | 
        BindingFlags.Static, null, null, new object [] {});
    

提交回复
热议问题