序列化列表与LT;>类声明的内部修饰符? [英] Serialize List<> of classes declared with internal modifier?

查看:138
本文介绍了序列化列表与LT;>类声明的内部修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想XML序列化添加到C#中的相当琐碎的类结构。从本质上讲,有一个根类(称之为ACLASS),其中包含一些其他类的几个实例列表的单个实例(称之为AnotherClass):

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; }
}



序列化时,我想为这两个类被序列化在一起 - 那就是,如果我序列ACLASS,其AnotherClass名单被序列化,以及(见的这个问题)。

我有这主要是合作,但问题是,序列化过程中,XmlSerializer的似乎只要处理之类的公共属性 - 它不序列AnotherClass在所有如果列表声明内部

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")]

似乎并没有做任何事情。有没有一种方法可以让我得到XmlSerializer的递归序列化被宣布内部对象的列表?

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?

推荐答案

您是在右边跟踪...除了实际序列不被的System.Xml执行,而是由一个动态生成组件。你无法预测组件(它是随机生成)的名称,所以你不能在 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.

唯一的办法是预先生成的XML序列化装配。你可以做到这一点使用 XML序列生成器工具(Sgen.exe)。生成的程序集的名称将是YourAssembly.XmlSerializers这是你在 InternalsVisibleTo 属性使用的名称。

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.

这篇关于序列化列表与LT;>类声明的内部修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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