gpt4 book ai didi

C# [XmlElement] 属性不隐式转换类型?

转载 作者:太空宇宙 更新时间:2023-11-03 13:42:08 25 4
gpt4 key购买 nike

所以我有一个类 Texture2DProcessor,它继承了 IXmlSerializable 并隐式转换为 Texture2D

public static implicit operator Texture2D(Texture2DProcessor o)
{
return o.Data;
}
public static implicit operator Texture2DProcessor(Texture2D o)
{
return o == null ? null : new Texture2DProcessor(o);
}

然后我有一个结构 GunProperties,它包含一个 Texture2D 属性和一个类型设置为 Texture2DProcessor 的 XmlElement 属性

Texture2D _sideSprite;
[XmlElement(typeof(Texture2DProcessor))]
public Texture2D SideSprite
{
get { return _sideSprite; }
set { _sideSprite = value; }
}

我收到以下运行时错误

Cannot serialize member '...GunProperties.SideSprite' of type 'Microsoft.Xna.Framework.Graphics.Texture2D'

为什么XmlSerializer不使用Texture2DProcessor读写Xml数据?

我还知道我的 ReadXml 和 WriteXml 方法有效,因为它们工作正常并且我能够使用纹理。

Texture2D texture;
XmlSerializer serializer = new XmlSerializer(typeof(Texture2DProcessor));
serializer.Deserialize(new FileStream(path, FileMode.Open), texture);

我遇到这个麻烦的原因是我正在使用 monogame,内容管道非常困惑,尤其是对于自定义类型,除此问题外,我一切正常。

最佳答案

看起来这可能是 Mono XmlSerializer 的限制。我有一个在 .NET 下运行的小型测试应用程序,但不能在 Mono 3.0.6 下运行。

但这看起来很容易变通:

[XmlIgnore]
public Texture2D SideSprite { get; set; }

[XmlElement("SideSprite")]
[EditorBrowsable(EditorBrowsableState.Never)]
public Texture2DProcessor SideSpriteProcessor
{
get { return SideSprite; }
set { SideSprite = value; }
}

我们在我的 Noda Time 项目中遇到了同样的问题,因为 XML 序列化不能很好地与不可变类型混合。我几乎已经给出了 same advice那里。

编辑:好的,下面是一些可以工作的示例代码:

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

public class Person
{
public string Name { get; set; }
}

public class PersonWrapper : IXmlSerializable
{
public Person Person { get; set; }

public static implicit operator Person(PersonWrapper wrapper)
{
return wrapper == null ? null : wrapper.Person;
}

public static implicit operator PersonWrapper(Person person)
{
return new PersonWrapper { Person = person };
}

public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
string name = reader.ReadString();
reader.ReadEndElement();
Person = new Person { Name = name };
}

public void WriteXml(XmlWriter writer)
{
writer.WriteString(Person.Name);
}
}

public class Company
{
[XmlElement(typeof(PersonWrapper))]
public Person Director { get; set; }

[XmlElement(typeof(PersonWrapper))]
public Person Manager { get; set; }
}

class Test
{
static void Main()
{
var serializer = new XmlSerializer(typeof(Company));

var original = new Company
{
Director = new Person { Name = "Holly" },
Manager = new Person { Name = "Jon" }
};

var writer = new StringWriter();
serializer.Serialize(writer, original);
Console.WriteLine("XML:");
Console.WriteLine(writer.ToString());

var reader = new StringReader(writer.ToString());
var deserialized = (Company) serializer.Deserialize(reader);

Console.WriteLine("Deserialized:");
Console.WriteLine("Director: {0}", deserialized.Director.Name);
Console.WriteLine("Manager: {0}", deserialized.Manager.Name);
}
}

关于C# [XmlElement] 属性不隐式转换类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621617/

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