Does the order of attributes have any guarantees?

前端 未结 3 693
情歌与酒
情歌与酒 2020-12-20 11:56

If multiple attributes are applied to a member, e.g.

[Foo]
[Bar]
void Baz() { ... }

Then are any guarantees made by the CLR/.NET specificat

相关标签:
3条回答
  • 2020-12-20 12:38

    No from 17.2 Attribute specification it does not seems so.

    The order in which attributes are specified in such a list, and the order in which sections attached to the same program entity are arranged, is not significant. For instance, the attribute specifications [A][B], [B][A], [A, B], and [B, A] are equivalent.

    0 讨论(0)
  • 2020-12-20 12:40

    imo Attributes shouldn't have an order as they're not flow control constructs.

    I guess you could add a priority property to the attribute and use that to determine the run order?

    0 讨论(0)
  • 2020-12-20 12:44

    Relying on ordering for this sort of thing is generally a bad idea. If the order is important, I would give the attribute a Priority property so that it can be made very explicit.

    I don't know of any guarantees that they will be retrieved in the original order.

    0 讨论(0)
提交回复
热议问题