惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

博客园 - james.dong

初次使用T4引擎生成数据库表实体 - james.dong - 博客园 加快大表关联查询的速度(oracle) DataTable 排序 - james.dong - 博客园 查询oracle数据库中所有视图和表的信息 日期验证正规表达式( YYYY-MM-DD YYYY-MM-DD hh:mm:ss YYYY/MM/DD) windows2000server下 iis 无法下载 .exe , .dll文件的解决办法。 - james.dong C#中datagridview使用技巧系列谈(-)让输入焦点从左到右收藏 oracle中创建自增字段 LotusScript基础知识(二) LotusScript基本语法知识(一) LotusScript中Option 的含义 Lotusscript中Instr()函数的功能和用法 System.Xml.XmlDocument.SelectNodes() 查询不到节点问题? - james.dong - 博客园 Combox控件实现类似TextBox控件的ReadOnly=true时的背景颜色和字体颜色!(WinForm) WPF 中的 BitmapEffect 的 各种样式 WPF相关文章汇总 使用List泛型,怎么排序 asp.net2.0解决用户控件图片相对路径出错的问题,ResolveUrl的用法 window.showModalDialog()时没有显示修改后的数据 - james.dong - 博客园
.net 序列化和反序列化自定义treenode类
james.dong · 2008-12-23 · via 博客园 - james.dong

要存储一个类型,序列化是最简单的一种实现方式,TreeView本省并不支持序列化,但是TreeNode支持,因此需要从TreeNode下手, 所有序列化TreeView其实就是序列化TreeNode.

1. 自定义一个类SerialzeTreeNode继承TreeNode继承并实现ISerializable接口,其实TreeNode已经实现了ISerializable接口,在实现这个接口时要调用基类TreeNode的Serialize函数,要不然序列化的节点会不全,在Serialize函数中添加自己的序列化逻辑, 

2. 重写所有的TreeNode构造函数,特别是与序列化有关的那个构造函数,public TreeNode(SerializationInfo serinfo, StreamingContext context)就是这个了,要不然就不能正确的反序列化了 
3. 有可能的话实现一个IDeserializationCallback接口,以便于在反序列化后初始化一些变量之类的动作,因为,反序列化只调用反序列化的那个构造函数. 
4. 添加泛型支持,其实也可以用Tag用来存储要序列化的值,但是Tag是object的,不是很好,因此增加一个泛型 
变量是很有意义的,这样便于强化类型也能够很好的利用IDE的智能感知功能. 
public class SerializTreeNode<T>:TreeNode,ISerializable 

  public T TTag; 

5. 如果要使用小图标的话还要做些工作,虽然查看反序列化中的数据,有ImageSelectedIndex项,但是却无效,因此在实现ISerializable接口中顺便把ImageSelectedIndex属性也序列化进去,然后在反序列化的时候设置其值,这样就可以保存其图片索引了.Key与Index冲突,只能一个有效,因此可以二选一,看TreeView是如何设置图片的了. 
6. 对自定义类加上Serializable可序列化标记,要不然都正确了还是无法正确工作的.,同时泛型的那个类也必须要标记为Serializable,否则也会出错. 
7. 扩展TreeView提供序列化和反序列化的函数以保存树节点和加载保存的树节点. 
  public interface ISerializeTreeView 
  { 
  bool SaveTreeView(string filename); 
  bool LoadTreeView(string filename); 
  } 
可以定义这样的一个接口  

二:拷贝对象到剪切板

Add object to Clipboard using C# in a couple of ways 

This is a simple example of using the ClipBoard to transport objects. In this example I will generate a TreeNode and save it to a ClipBoard entry. I will then retrieve this node, cast it and either add it to the root of the TreeView or add it to the selected node's child nodes.

Method 1

Step 1: Copy new TreeNode to Clipboard
  private void button1_Click(object sender, EventArgs e)
  {
  TreeNode t = new TreeNode("ClipBoardNode");
  Clipboard.SetData("TreeNode", t);
  }

Step 2: Paste new TreeNode from Clipboard to TreeView
 
  private void button2_Click(object sender, EventArgs e)
  {
  TreeNode t = (TreeNode)Clipboard.GetData("TreeNode");
  if (treeView1.SelectedNode == null)
  treeView1.Nodes.Add(t);
  else
  treeView1.SelectedNode.Nodes.Add(t);
  }

Method 2

Another way to append to the Clipboard is the method SetDataObject and GetDataObject. There is however some difference here.
You do not provide a key, you simply save it to a designated location
You are limited to the objects which you cast the returned object back into as you can see from the image below.

To solve this I made two simple helper methods which would serialize the object to a binary representation and then get the base64String of that data, thus giving me a string object. I then put this into the ClipBoard and going the other way I did the opposite. Converted the base64String into a byte array, instantiated a MemoryStream with it and then deserialized the data returning an object. It is then up to the calling code to cast the object.
The helper methods

 
  private string Serialize(object objectToSerialize)
  {
  string serialString = null;
  using (System.IO.MemoryStream ms1 = new System.IO.MemoryStream())
  {
  BinaryFormatter b = new BinaryFormatter();
  b.Serialize(ms1, objectToSerialize);
  byte[] arrayByte = ms1.ToArray();
  serialString = Convert.ToBase64String(arrayByte);
  }
  return serialString;
  }
  private object DeSerialize(string serializationString)
  {
  object deserialObject = null;
  byte[] arrayByte = Convert.FromBase64String(serializationString);
  using (System.IO.MemoryStream ms1 = new System.IO.MemoryStream(arrayByte))
  {
  BinaryFormatter b = new BinaryFormatter();
  deserialObject = b.Deserialize(ms1);
  }
  return deserialObject;
  }
Method Code 2 Steps

Step 1: Copy new TreeNode to Clipboard
  private void button1_Click_1(object sender, EventArgs e)
  {
  TreeNode t = new TreeNode("ClipBoardNode");
  Clipboard.SetData("TreeNode", Serialize(t));
  }

Step 2: Paste new TreeNode from Clipboard to TreeView
  private void button2_Click(object sender, EventArgs e)
  {
  TreeNode t = (TreeNode)DeSerialize(Clipboard.GetData("TreeNode").ToString());
  if (treeView1.SelectedNode == null)
  treeView1.Nodes.Add(t);
  else
  treeView1.SelectedNode.Nodes.Add(t);
  }