简体   繁体   English

如何使用TextRange属性序列化类?

[英]How to serialize a class with a TextRange property?

I'm trying to serialize a class that has a property of type TextRange in it. 我正在尝试序列化其中具有TextRange类型的属性的类。

Example: 例:

public class MyClass
{
    private string someProp;
    public string SomeProp
    {
        get { return someProp; }
        set { someProp = value; }
    }

    private TextRange myTextRange;
    public TextRange MyTextRange
    {
        get { return myTextRange; }
        set { myTextRange = value; }
    }
}

The thing is, that the TextRange type can't be serialized regularly while serializing the whole class, it has a special method of its own for serializing itself, i'm doing it like this: 事实是,在序列化整个类时,不能定期序列化TextRange类型,它具有自己的特殊方法来序列化自身,我这样做是这样的:

using (MemoryStream ms = new MemoryStream())
{
    myTextRange.Save(ms, DataFormats.Xaml, true);
    string xaml = Encoding.ASCII.GetString(ms.ToArray());
}

The problem is that I want the class to be serialized into one string (xml string) with the TextRange property and the other property together. 问题是我希望将类与TextRange属性和另一个属性一起序列化为一个字符串(xml字符串)。 I don't mind using another serialization method (not xml) but I don't see how it solves the problem. 我不介意使用另一种序列化方法(不是xml),但是我看不到它如何解决问题。

Regularly I serialize the whole class at once, but the TextRange class is not marked as serializable (no binary serilaization) and doesn't have an empty constructor (no xml serilization). 我定期地一次序列化整个类,但是TextRange类没有被标记为可序列化的(没有二进制序列化),并且没有空的构造函数(没有xml序列化)。

That's how I do it regularly: 这就是我经常这样做的方式:

XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
StringWriter stringWriter = new StringWriter();
using (XmlWriter writer = XmlWriter.Create(stringWriter))
{
    serializer.Serialize(writer, this);
    string xml = stringWriter.ToString();
    return xml;
}

How can I do this? 我怎样才能做到这一点?

Well, for anyone interested. 好吧,对于任何有兴趣的人。 The problem was that i wanted to have the whole MyClass object serialized to one xml string. 问题是我想将整个MyClass对象序列化为一个xml字符串。 But the TextRange requires special serializing method like i wrote in the main post. 但是TextRange需要特殊的序列化方法,就像我在主帖子中写的那样。 So the workaround was: 因此,解决方法是:

  • Create a public helper class inside MyClass, say MyClassSerializationHelper. 在MyClass内创建一个公共帮助程序类,例如MyClassSerializationHelper。

  • Add two string properties to it, same as MyClass properties but as strings. 向其添加两个字符串属性,与MyClass属性相同,但作为字符串。 In my case SomeProp and MyTextRange. 就我而言,是SomeProp和MyTextRange。 Full properties (getters and setters). 完整属性(获取器和设置器)。

To Serialize: 序列化:

  • Create a MyClassSerializationHelper instance. 创建一个MyClassSerializationHelper实例。

  • SomeProp gets the same value from MyClass SomeProp property, because it's a string. SomeProp从MyClass SomeProp属性获取相同的值,因为它是一个字符串。

  • MyTextRange gets the serialized string of the MyClass MyTextRange property using the special serialization method. MyTextRange使用特殊的序列化方法获取MyClass MyTextRange属性的序列化字符串

  • Serialize the whole MyClassSerializationHelper instance, you got an xml string. 序列化整个MyClassSerializationHelper实例,您得到一个xml字符串。

To Deserialize: 反序列化:

  • Deserialize the xml string and get a MyClassSerializationHelper instace. 反序列化xml字符串并获取MyClassSerializationHelper实例。

  • Deserialize the MyTextRange property (it's an xml string) to get a TextRange instance. 反序列化MyTextRange属性(它是一个xml字符串)以获取TextRange实例。

  • Rebuild a MyClass instance using the properties you deserialized. 使用反序列化的属性重建MyClass实例。

Hope that's clear. 希望很清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM