如何序列化点,笔和画笔? [英] How to Serialize Point, Pen, and Brush?

查看:69
本文介绍了如何序列化点,笔和画笔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义链接列表,可以在其中序列化Pen,Point等的实例.
但是,如果我在简单的类中使用Point and Pen对象(没有链接列表),则无法序列化Point and Pen的实例.

链表类是这样的


I am using custom linked list, where I can able to serialize instance of Pen, Point etc.
But if I take Point and Pen object in simple class (without linked list), I can not serialize instance of Point and Pen.

Linked List class is like this


[Serializable]
public class LinkedList
{
    // various methods to maintain First, Last, Next, Previous Node information like
    public void Add(Node node)
      {
        ......
    }
    public void Remove(Node node)
      {
        ......
    }

    //Node classs
    [Serializable]
      public class Node
      {
        public Point PointData
            {
                get;
                set;
            }
        public Node Next
            {
                get;
            set;
            }
    }
}



我可以使用以下代码序列化LinkedList实例



I can serialize LinkedList instance using below code

BinaryFormatter formatter = new BinaryFormatter()
formatter.Serialize(file, objLinkedList);



它工作正常,但是如果我使用类似
的类



It is working fine, but if I use class like

[Serializable]
 public class OverlayPoint
 {
    public Point ObjPoint
       {
            get;
            set;
       }
       public Pen ObjPen
       {
            get;
            set;
       }
}



然后我无法序列化显示错误,如



Then I cannot serialize showing error like

Type ''System.Drawing.Pen'' in Assembly ''System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'' is not marked as serializable.


仅仅因为链表中的内部类,它就不会触发错误.
我不明白为什么会这样

感谢您的宝贵时间


Just because of internal class in linked list it is not firing error.
I could not able understand why this happened

Thanks for your time

推荐答案



我使用以下扩展将对象序列化为字符串.

Hi,

I use the following Extension to Serialize object to string.

public static string SerializeObjectToString<T>(this T objectToSerialize)
        {
            StringWriter outStream = new StringWriter();
            string value;
            try
            {
                XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
                s.Serialize(outStream, objectToSerialize);
                value = outStream.ToString();
            }
            finally
            {
                outStream.Close();
            }
            return value;
        }



然后序列化您的对象,您将使用以下方法.



To then serialize your object you would use like follow.

Node myNode = new Node {PointData = new Point(10, 50), Next = new Node {PointData = new Point(30, 50)}};

Console.WriteLine(myNode.SerializeObjectToString());



在下面查看结果.



View result below.

<br />
"<?xml version=\"1.0\" encoding=\"utf-16\"?><br />
<Node xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">  <PointData><br />
    <X>10</X><br />
    <Y>50</Y><br />
    </PointData><br />
    <Next><br />
        <PointData><br />
            <X>30</X><br />
            <Y>50</Y><br />
        </PointData><br />
    </Next><br />
</Node><br />



希望这会有所帮助.



Hope this helps.


这篇关于如何序列化点,笔和画笔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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