【发布时间】:2015-04-04 04:54:46
【问题描述】:
我想对使用 XmlSerializer.Deserialize 创建的 CustomType 的所有实例进行一些操作
最好的方法是作为构造函数传入,但是“PostSerialization”可以正常工作。 在此示例中,我需要基于 XmlSerializer 上下文将一个对象传递给我的 CustomType。
public class CustomType : IXmlSerializable
{
public CustomType()
: this(null)
{
}
public CustomType(Object somethingImportant)
{
}
public void ReadXml(System.Xml.XmlReader reader) { ... }
public void WriteXml(System.Xml.XmlWriter writer) { ... }
}
...
Object somethingImportant = 12345; // I need to pass this to *all* CustomType
var serializer = new XmlSerializer(typeof(MyType));
var reader = new StringReader(str);
return serializer.Deserialize(reader) as MyType;
XmlAttributeOverrides 是个好主意,但是“MyType”相当复杂,我无法通过所有可能的 XmlElement 来自定义创建 CustomType。
【问题讨论】:
-
CustomType和MyType是什么关系? -
@CoderDennis MyType 中某个地方的一些孩子的孩子。示例类 MyType { public CustomType A {get;set;} },但更复杂。 (见我问题的最后一句话)
-
好的,但我仍然不知道您在寻找什么。如果您不与我们分享任何复杂性,我们将无法帮助您解决复杂性。
-
其实,你能不能把
Object somethingImportantthread-static,在无参构造函数中非空时使用? -
@dbc 这是一个丑陋/伟大的解决方案 dbc。您能否将其作为以下解决方案提出。我会采用那个解决方案。
标签: c# xml xmlserializer