How can I add a type constraint to include anything serializable in a generic method?

前端 未结 4 1662
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 09:45

My generic method needs to serialize the object passed to it, however just insisting that it implements ISerializable doesn\'t seem to work. For example, I have a struct ret

4条回答
  •  甜味超标
    2021-01-01 10:45

    I wrote a length blog article on this subject that you may find helpful. It mainly goes into binary serialization but the concepts are applicable to most any serialization format.

    • http://blogs.msdn.com/jaredpar/archive/2009/03/31/is-it-serializable.aspx

    The long and short of it is

    • There is no way to add a reliable generic constraint
    • The only way to check and see if an object was serializable is to serialize it and see if the operation succeeds

提交回复
热议问题