数据契约序列化是否使用反射?
XmlSerialization 为每个类创建一个序列化器代理。代理驻留在不同的程序集中,因此它只能序列化公共字段。
DataContract 序列化也可以序列化私有字段。这是否意味着它使用反射?是不是比使用代理慢(除了第一次)?
XmlSerialization creates a serializer proxy for each class. The proxy resides in a different assembly so it can serialize only public fields.
DataContract serialization can serialize private fields too. Does it mean it uses reflection? Isn't it slower than using a proxy (except for the first time)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此 protobuf 网页 显示了
DataContractSerializer< 的性能/code> 明显优于
XmlSerializer
。当然,您应该始终使用自己的数据进行测试,但如果您希望替换XmlSerializer
,您很可能会发现DataContractSerializer
可以提高性能。我不确定 DataContractSerializer 是如何在内部实现的,但通常序列化器是高度优化的。对于 DataContractSerializer 来说尤其如此,因为它是 WCF 性能图的重要组成部分。序列化器动态生成 MSIL 代码的情况并不罕见。完成此操作后,
DynamicMethod
允许您(令人惊讶!)绕过可见性检查(请参阅MSDN),因此可以在不反射的情况下访问私有字段。来自 MSDN:
This protobuf-net page shows the performance of
DataContractSerializer
to be significantly better thanXmlSerializer
. Of course, you should always test with your own data, but if you are looking to replaceXmlSerializer
, you will most likely findDataContractSerializer
to be a performance improvement.I'm not sure how
DataContractSerializer
is implemented internally, but generally serializers are highly optimized. This is especially true forDataContractSerializer
since it is a big part of the performance picture for WCF. It is not uncommon for a serializer to generate MSIL code on the fly. When this is done,DynamicMethod
allows you to (surprisingly!) bypass visibility checks (see MSDN), so it is possible to access private fields without reflection.From MSDN: