如何最好地判断泛型类型参数是否可以由 protobuf-net 序列化?
我正在编写一些自定义通用数据结构。如何确保传入的泛型类型可以被protobuf-net序列化?由于 protobuf-net 不依赖于接口,因此我无法以这种方式限制类型参数。我考虑过对构造函数内的类型进行运行时测试,但我什至无法使用 PrepareSerializer
因为这依赖于该类型是引用类型。我可以简单地尝试序列化和反序列化,但这充其量看起来很混乱。验证给定泛型类型可以序列化的最佳方法是什么?
I am writing some custom generic data structures. How can I ensure that the generic type passed in can be serialized by protobuf-net? Since protobuf-net doesn't rely on interfaces, I can't constrain the type parameter in that way. I thought about runtime testing of the type inside the constructor, but I can't even use PrepareSerializer<T>
because that relies on the type being a reference type. I could simply attempting a serialization and deserialization, but this seems messy at best. What's the best way to verify the a given generic type can be serialized?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XmlProtoSerializer 必须做出类似的决定。例如:
如果返回负值,则无法序列化。我可以在更受支持的
bool CanSerialize(Type)
中提供此功能,如果这会有帮助的话?XmlProtoSerializer has to make a similar determination. For example:
If that returns a negative value it can't be serialised. I could make this available in a more supported
bool CanSerialize(Type)
if that would be helpful?