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

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

    Instead of

    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

    try

    XmlSerializer xmlSerializer = new XmlSerializer(message.GetType());

提交回复
热议问题