我正在尝试将 XML 序列化添加到 C# 中相当简单的类结构中。本质上,有一个根类的单个实例(称为 AClass),它包含一些其他类(称为 AnotherClass)的多个实例的列表:
[XmlRoot("RootNode")]
public class AClass {
[XmlElement("ListNode")]
internal List otherObjects { get; set; }
}
public class AnotherClass {
[XmlAttribute("Name")]
internal string name { get; set; }
}
序列化时,我希望这两个类一起序列化 - 即是,如果我序列化 AClass,它的 AnotherClass 列表也会被序列化(请参阅 这个问题)。
我基本上可以正常工作,但问题是在序列化期间,XmlSerializer 似乎只想处理类的 public 属性 - 如果声明了列表,它根本不会序列化 AnotherClass >内部。
我尝试使程序集的内部结构对序列化器可见:
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Xml")]
这似乎没有做任何事情。有没有办法让 XmlSerializer 递归序列化声明为内部的对象列表?
I'm trying to add XML serialization to a fairly trivial class structure in C#. Essentially, there's a single instance of a root class (call it AClass), which holds a List of several instances of some other class (call it AnotherClass):
[XmlRoot("RootNode")]
public class AClass {
[XmlElement("ListNode")]
internal List otherObjects { get; set; }
}
public class AnotherClass {
[XmlAttribute("Name")]
internal string name { get; set; }
}
When serializing, I'd like for both these classes to be serialized together - that is, if I serialize AClass, its list of AnotherClass gets serialized as well (see this question).
I have this mostly working, but the problem is that during serialization, XmlSerializer only seems to want to deal with public
properties of the class - it doesn't serialize AnotherClass at all if the list is declared internal
.
I tried making the assembly's internals visible to the serializer:
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Xml")]
That didn't seem to do anything. Is there a way I can get XmlSerializer to recursively serialize lists of objects that are declared internal?
发布评论
评论(2)
您走在正确的轨道上...只不过实际的序列化不是由 System.Xml 执行的,而是由动态生成的程序集执行的。您无法预测该程序集的名称(它是随机生成的),因此您无法在
InternalsVisibleTo
属性中使用它。唯一的解决方案是预先生成 XML 序列化程序集。您可以使用 XML 序列化程序生成器工具 (Sgen.exe) 来完成此操作。生成的程序集的名称将为“YourAssembly.XmlSerializers”;这是您必须在
InternalsVisibleTo
属性中使用的名称。You're on the right track... except that the actual serialization is not performed by System.Xml, but by a dynamically generated assembly. You can't predict the name of that assembly (it's randomly generated), so you can't use it in the
InternalsVisibleTo
attribute.The only solution is to pre-generate the XML serialization assembly. You can do that using the XML Serializer Generator Tool (Sgen.exe). The name of the generated assembly will be "YourAssembly.XmlSerializers" ; that's the name you have to use in the
InternalsVisibleTo
attribute.添加
InternalsVisibleTo
属性的想法是一个很好的想法,但我认为问题在于 Xml 序列化代码仅查找程序集中的公共类型。据我所知,无法使 XmlSerializer 序列化或反序列化内部类型,您必须将类型声明为公共类型或编写自己的序列化代码。
此外, XmlSerializer 类文档 明确指出只有对象的公共属性才会被序列化:“XML 序列化是将对象的公共属性和字段转换为串行格式(在本例中为 XML)以进行存储或传输的过程”,因此它看起来确实是一种设计决定。
The idea of adding the
InternalsVisibleTo
attribute is a good one, but I think that the problem is that the Xml serialization code only looks for the public types in the assembly.To my knowledge there is no way to make XmlSerializer serialize or deserialize internal types, and you will have to either declare the types as public or write your own serialization code.
Moreover, the XmlSerializer class documentation explicitly states that only public properties of the object will be serialized: "XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport", so it really looks like it's a design decision.