How to check if a type is marked with an attribute?

前端 未结 1 1535
春和景丽
春和景丽 2021-01-25 16:59

Does it needs reflection?

1条回答
  •  太阳男子
    2021-01-25 17:19

    Yes:

    Attribute.IsDefined(yourType, attributeType);
    

    Except for [Serializable], which might also be yourType.IsSerializable (if you check the IL, [Serializable] doesn't actually get written as an attribute - there is a special type modifier; either approach works fine, though).

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