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

后端 未结 2 981
无人共我
无人共我 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.

提交回复
热议问题