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

前端 未结 4 1668
爱一瞬间的悲伤
爱一瞬间的悲伤 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:33

    The only way to know if an object is serializable is to try to serialize it.

    In fact, you were asking how to tell if a type "is serializable", but the actual question will be with respect to objects. Some instances of a type may not be serializable even if the type is marked [Serializable]. For instance, what if the instance contains circular references?

提交回复
热议问题