gpt4 book ai didi

c# - 在 IXmlSerializable 方法中访问 XmlAttributesOverrides 添加的属性

转载 作者:太空狗 更新时间:2023-10-29 23:53:05 27 4
gpt4 key购买 nike

如何使用 XmlAttributesOverrides 访问应用于 IXmlSerializable 对象中字段的 XmlAttributes

示例 IXmlSerializable 对象:

    public class Person : SomeBaseClass, IXmlSerializable
{
public string Name1;

public string Name2;

[XmlIgnore]
public string Name3;

public Person()
{
}

public Person(string first, string second, string third)
{
Name1 = first;
Name2 = second;
Name3 = third;
}

public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
// ....
}

public void WriteXml(XmlWriter writer)
{
FieldInfo[] finfo = this.GetType().GetFields();

foreach (FieldInfo finf in finfo)
{
FieldAttributes attr = finf.Attributes;
object[] atts = finf.GetCustomAttributes(true);

if (atts.Length == 0)
{
// handle field with no attributes ... should be just Name1
// but also get Name2 since XmlAttributOverrides' XmlIgnore is not
// included with GetCustomAttributes results.
writer.WriteElementString(finf.Name, (string)finf.GetValue(this));
}
else
{
// handle field with attributes ... should be Name2 and Name3
// but only get Name3 via [XmlIgnore] compiler generated attribute
}
}
}
}

典型的覆盖创建:

        // parent app ...

public XmlSerializer CreateOverrider()
{
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();

attrs.XmlIgnore = true;
xOver.Add(typeof(Person), "name2", attrs);

XmlSerializer xSer = new XmlSerializer(typeof(Person), xOver);
return xSer;
}

private void button2_Click(object sender, EventArgs e)
{
// Custom XmlSerialize
Person pson = new Person("First", "Second", "Third");

XmlSerializer serializer = CreateOverrider();
TextWriter writer = new StreamWriter("PersonOverride.xml");

serializer.Serialize(writer, pson);
writer.Close();
}

// etc ...

创建的输出:

<?xml version="1.0" encoding="utf-8"?><Person><Name1>First</Name1><Name2>Second</Name2></Person>

我需要使用 IXmlSerializable 来克服“SomeBaseClass”的继承问题。

问题是 GetCustomArributes 没有返回添加到 XmlAttributeOverrides 集合的任何属性 - 或者我做错了!?

也有可能 GetCustomAttributes 不应该返回此类添加的属性,或者我不应该在 IXmlSerializable 中使用 XmlAttributeOverrides > 类。

所以...任何想法或替代方案。感谢您抽出时间。

最佳答案

没有办法做到这一点。

原因是因为 XmlSerializer 将在给定不是 IXmlSerializable 的对象时生成序列化程序类。这些 XML 覆盖属性将用于以不同方式编译这些类。 XML 覆盖属性在序列化或反序列化期间不在运行时应用;这就是它们无法访问的原因。

继承 IXmlSerializable 的类不会生成序列化程序类。如果您想使用 XML 覆盖属性,那么您将不必覆盖序列化程序类编译器。改为使用 Person 的实现,让它根据给定的覆盖为​​您生成序列化程序类(也将运行速度快很多倍):

public class Person : SomeBaseClass
{
public string Name1;

public string Name2;

[XmlIgnore]
public string Name3;

public Person()
{
}

public Person(string first, string second, string third)
{
Name1 = first;
Name2 = second;
Name3 = third;
}
}

当然也欢迎您编写自己的序列化程序类编译器,但这比这里能容纳的要复杂一些。但实现应该看起来像这样:

public static Type GeneratePersonSerializer(XmlAttributeOverrides overrides) {
//here compile a class to generate a Type inheriting from IXmlSerializable
//the serializer logic in this class should be generated by taking into
//account the given XmlAttributeOverrides
//the type returned should be the Type passed into new XmlSerializer(Type type)
}

关于c# - 在 IXmlSerializable 方法中访问 XmlAttributesOverrides 添加的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4329112/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com