Is it possible to have a compile time check that a type is marked with the Serializable attribute

后端 未结 2 976
无人共我
无人共我 2021-01-07 15:27

Specifically we\'re making our application compatible with the Out Of Process Session State server where all types saved in session must be serializable.

Is there

相关标签:
2条回答
  • 2021-01-07 15:44

    No, there's no kind of generic constraint like that. Attributes are generally meant to indicate something about the implementation of a type. You'll have to either introduce your own marker interface (urgh) or insert an execution-time check and hope that your testing is sufficient.

    Eric Lippert's post about properties and attributes is good to read here. It's not quite the same question, as it's talking more about objects vs types, but it's still relevant in terms of attributes expressing mechanics.

    0 讨论(0)
  • 2021-01-07 15:47

    You could write a custom FxCop rule to generate warnings for this scenario.

    Jason Block has a sample rule on his site.

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