如何序列化控件

一杯清茶几行代码 2005-08-30 04:48:31
需要序列化控件,例如TextBox
XML序列化和二进制序列化都不行,请教各位是怎么做这方面的?
...全文
362 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kfy0002 2005-12-26
  • 打赏
  • 举报
回复
设计器中,用序列化保存设计态控件,好像有点难,用反射写/读更好控制些
kfy0002 2005-12-26
  • 打赏
  • 举报
回复
private void ReadProperty(XmlNode node, object instance, ArrayList errors)
{
XmlAttribute nameAttr = node.Attributes["name"];
if (nameAttr == null)
{
errors.Add("Property has no name");
return;
}

PropertyDescriptor prop = TypeDescriptor.GetProperties(instance)[nameAttr.Value];
if (prop == null)
{
errors.Add(string.Format("Property {0} does not exist on {1}", nameAttr.Value, instance.GetType().FullName));
return;
}

// Get the type of this property. We have three options:
// 1. A normal read/write property.
// 2. A "Content" property.
// 3. A collection.
//
bool isContent = prop.Attributes.Contains(DesignerSerializationVisibilityAttribute.Content);

if (isContent)
{
object value = prop.GetValue(instance);

// Handle the case of a content property that is a collection.
//
if (value is IList)
{
foreach(XmlNode child in node.ChildNodes)
{
if (child.Name.Equals("Item"))
{
object item;
XmlAttribute typeAttr = child.Attributes["type"];
if (typeAttr == null)
{
errors.Add("Item has no type attribute");
continue;
}

Type type = Type.GetType(typeAttr.Value);
if (type == null)
{
errors.Add(string.Format("Item type {0} could not be found.", typeAttr.Value));
continue;
}

if (ReadValue(child, TypeDescriptor.GetConverter(type), errors, out item))
{
try
{
((IList)value).Add(item);
}
catch(Exception ex)
{
errors.Add(ex.Message);
}
}
}
else
{
errors.Add(string.Format("Only Item elements are allowed in collections, not {0} elements.", child.Name));
}
}
}
else
{
// Handle the case of a content property that consists of child properties.
//
foreach(XmlNode child in node.ChildNodes)
{
if (child.Name.Equals("Property"))
{
ReadProperty(child, value, errors);
}
else
{
errors.Add(string.Format("Only Property elements are allowed in content properties, not {0} elements.", child.Name));
}
}
}
}
else
{
object value;
if (ReadValue(node, prop.Converter, errors, out value))
{
// ReadValue succeeded. Fill in the property value.
//
try
{
prop.SetValue(instance, value);
}
catch(Exception ex)
{
errors.Add(ex.Message);
}
}
}
}
kfy0002 2005-12-26
  • 打赏
  • 举报
回复
private object ReadObject(XmlNode node, ArrayList errors)
{
XmlAttribute typeAttr = node.Attributes["type"];
if (typeAttr == null)
{
errors.Add("<Object> tag is missing required type attribute");
return null;
}

Type type = Type.GetType(typeAttr.Value);
if (type == null)
{
errors.Add(string.Format("Type {0} could not be loaded.", typeAttr.Value));
return null;
}

// This can be null if there is no name for the object.
//
XmlAttribute nameAttr = node.Attributes["name"];

object instance;

if (typeof(IComponent).IsAssignableFrom(type))
{
if (nameAttr == null)
{
instance = host.CreateComponent(type);
}
else
{
instance = host.CreateComponent(type, nameAttr.Value);
}
}
else
{
instance = Activator.CreateInstance(type);
}

// Got an object, now we must process it. Check to see if this tag
// offers a child collection for us to add children to.
//
XmlAttribute childAttr = node.Attributes["children"];
IList childList = null;
if (childAttr != null)
{
PropertyDescriptor childProp = TypeDescriptor.GetProperties(instance)[childAttr.Value];
if (childProp == null)
{
errors.Add(string.Format("The children attribute lists {0} as the child collection but this is not a property on {1}", childAttr.Value, instance.GetType().FullName));
}
else
{
childList = childProp.GetValue(instance) as IList;
if (childList == null)
{
errors.Add(string.Format("The property {0} was found but did not return a valid IList", childProp.Name));
}
}
}

// Now, walk the rest of the tags under this element.
//
foreach(XmlNode childNode in node.ChildNodes)
{
if (childNode.Name.Equals("Object"))
{
// Another object. In this case, create the object, and
// parent it to ours using the children property. If there
// is no children property, bail out now.
if (childAttr == null)
{
errors.Add("Child object found but there is no children attribute");
continue;
}

// no sense doing this if there was an error getting the property. We've already reported the
// error above.
if (childList != null)
{
object childInstance = ReadObject(childNode, errors);
childList.Add(childInstance);
}
}
else if (childNode.Name.Equals("Property"))
{
// A property. Ask the property to parse itself.
//
ReadProperty(childNode, instance, errors);
}
else if (childNode.Name.Equals("Event"))
{
// An event. Ask the event to parse itself.
//
//ReadEvent(childNode, instance, errors);
}
}
return instance;
}
HowcanIdo 2005-12-20
  • 打赏
  • 举报
回复
不知道楼主的问题有没有解决?
to 楼上:告诉你一个应用
我目前在做一个设计器,运行时,用户会拖一些不同的控件放到工作区,那我如何保存这些控件的状态呢。
目前所能知道的方法就是楼上有位朋友说的,保存控件的数据,但是针对每种控件写是不是太复杂呢
levinknight 2005-09-11
  • 打赏
  • 举报
回复
不明白你为什么要序列化控件
wy231 2005-09-10
  • 打赏
  • 举报
回复
如果一个控件的状态在使用的过程中改变了,比如它的外观,大小等属性被改变,就需要保存改变后的状态,下次打开的时候反序列化一下,加载上次的状态!怎么做?
hatita 2005-08-30
  • 打赏
  • 举报
回复
想简单的序列化是不可能的

只有通过递归遍历
http://www.codeproject.com/vb/net/TreeViewDataAccess.asp
linuxyf 2005-08-30
  • 打赏
  • 举报
回复
请问序列化控件有必要吗?

同一种控件,除了数据设置不同,其它不都一样吗?请问你序列化这个控件的目的是什么?如果你只想保存它的数据,那直接序列化数据不就完了,用的时候你再New一个控件,把相应的数据设置就OK了。

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧