XML 序列化与 C# 中的反射 [英] XML Serialization vs Reflection in C#

查看:56
本文介绍了XML 序列化与 C# 中的反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 MSDN 的 XML 序列化:

序列化和反序列化对象进出 XML 文档.这XmlSerializer 使您能够控制对象如何编码为 XML.

Serializes and deserializes objects into and from XML documents. The XmlSerializer enables you to control how objects are encoded into XML.

来自 MSDN 的反思

反射提供对象(类型为类型)封装组件,模块和类型.您可以使用反射来动态创建一个类型的实例,将类型绑定到现有对象,或获取类型从现有对象并调用其方法或访问其字段和属性.如果您正在使用代码中的属性,反射使您能够访问它们.

Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them.

据我所知,我可以在运行时使用 XML 序列化创建对象吗?换句话说,假设我有一个数据库,我可以在几个表中定义我的类"或对象".然后我可以获取对象数据的 XML,然后在运行时创建对象.

As far as my understanding goes, I could create objects in run time using XML Serialization? In other words, let's say I have a database, I could define my "classes" or "objects" in couple of tables. I could then get the XML for the object's data and then create the object at run-time.

我也可以将这些对象编译为现成的库,然后使用反射来访问它的功能.

I could also already have those objects compiled as libraries readily available and then use Reflection to access it's functions.

根据您的理解,这两个概念中的哪一个可以提供最大的灵活性,同时牺牲最少的性能?如果您能提供带有注意事项的详细解释以及可能的代码示例,则可以加分.

From your understanding, which one of these two concepts would grant the most flexibility while sacrificing the least performance? Bonus points if you can provide a detailed explanation with considerations and perhaps a sample of code.

推荐答案

序列化和反射并不相互排斥.你绝对可以序列化和反序列化一个对象,然后使用反射修改它.

Serialization and Reflection are not mutually exclusive. You could definitely serialize and deserialize an object and then subsequently modify it using Reflection.

序列化

序列化只是拍摄对象状态的快照"的概念,以便您可以在以后恢复该快照.

Serialization is simply the concept of taking a 'snapshot' of an object's state so that you can potentially restore that snapshot at a later time.

如果您希望将对象存储在持久存储中,如果您不需要能够在特定值之后进行查询,那么序列化是一个不错的选择.

If you wish to store objects in a persistent store, serialization is a good option if you don't need to be able to query after particular values.

请注意,至少有两种不同类型的序列化:

Note that there are at least two different types of serialization:

  • XML 序列化,将对象表示为 XML.由于它是 XML,因此这种表示(至少在理论上)是人类可读和可互操作的.
  • 二进制序列化,它只是将对象作为字节数组存储和读取.这种表示是专有的,不是人类可读的.
  • XML Serialization, that represents an object as XML. Since it is XML, this representation is (in theory at least) human-readable and interoperable.
  • Binary serialization, that simply stores and reads an object as an array of bytes. This representation is proprietary and not human-readable.

反思

反射是使用对象元数据来操作对象的能力.例如,您可以决定将字符串Foo"分配给给定对象的所有可写字符串属性,而不考虑对象的类型.

Reflection is the ability to use object metadata to manipulate an object. You could, for example, decide that you want to assign the string "Foo" to all writable string properties of a given object, irrespective of the type of object.

当在设计时不知道对象的类型时,这很有趣.

This is mostly interesting when the type of object is not known at design time.

这篇关于XML 序列化与 C# 中的反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆