C# Enums with Flags Attribute

后端 未结 4 1189
难免孤独
难免孤独 2021-02-14 09:54

I was wondering if Enums with Flag attribute are mostly used for Bitwise operations why not the compilers autogenerate the values if the enum values as not defined.

For

4条回答
  •  不要未来只要你来
    2021-02-14 10:44

    I expect it is because the FlagsAttribute instance is being compiled alongside, or after, the Enum. That is to say decorating an object with an atribute (like [Flags]) causes the creation of an attribute object, it doesn't modify the base object in a fundamental way.

    Also, part of the information stored (for run-time instantiation of the attribute) is the entity to which it refers. It may be that the entity enum must be compiled before its attributes, so an attribute couldn't affect the entity it refers to (in this case enum). I don't know this statement to be true, it's just a guess.

    The big take-away is the attributes, like [Flags], are actually entities themselves and not modifications of the decorated type.

提交回复
热议问题