求教XmlSerializer问题,没有分哦,不过还请大家帮助

justseven 2008-03-24 04:41:12
我想把WINFORM的控件的属性放入XML里,在窗体加载的时候读取XML,获得控件属性,然后对各个空间赋值
因为控件内的好多属性无法序列化,所以我就使用了一个自定义类

[Serializable]
public class ControlModel
{

//Name 获取或设置控件的名称。 string
private string name;

public string Name
{
get { return name; }
set { name = value; }
}

//Type 获取或设置此控件类型 string
private string type;

public string Type
{
get { return type; }
set { type = value; }
}
//自动滚动
//private bool autoScroll;

//public bool AutoScroll
//{
// get { return autoScroll; }
// set { autoScroll = value; }
//}

//获取或设置在控件中显示的背景颜色。
private MyColor backColor;

public MyColor BackColor
{
get { return backColor; }
set { backColor = value; }
}

//获取或设置在控件中显示的背景图像。
//private Image backgroundImage;

//public Image BackgroundImage
//{
// get { return backgroundImage; }
// set { backgroundImage = value; }
//}

//Bottom 获取控件下边缘与其容器的工作区上边缘之间的距离(以像素为单位)。int
private int bottom;

public int Bottom
{
get { return bottom; }
set { bottom = value; }
}

//CanFocus 获取一个值,该值指示控件是否可以接收焦点。 bool
private bool canFocus;

public bool CanFocus
{
get { return canFocus; }
set { canFocus = value; }
}

//CanSelect 获取一个值,该值指示是否可以选中控件。 bool
private bool canSelect;

public bool CanSelect
{
get { return canSelect; }
set { canSelect = value; }
}


//Controls 获取包含在控件内的控件的集合。 Control.ControlCollection
private ModelCollection controls;

public ModelCollection Controls
{
get { return controls; }
set { controls = value; }
}

//DefaultBackColor 获取控件的默认背景色。 Color
//private Color defaultBackColor;

//public Color DefaultBackColor
//{
// get { return defaultBackColor; }
// set { defaultBackColor = value; }
//}

////DefaultFont 获取控件的默认字体。 Font
//private Font defaultFont;

//public Font DefaultFont
//{
// get { return defaultFont; }
// set { defaultFont = value; }
//}

////DefaultForeColor 获取控件的默认前景色。 Color
//private Color defaultForeColor;

//public Color DefaultForeColor
//{
// get { return defaultForeColor; }
// set { defaultForeColor = value; }
//}

//Dock 获取或设置哪些控件边框停靠到其父控件并确定控件如何随其父级一起调整大小。DockStyle
private DockStyle dock;

public DockStyle Dock
{
get { return dock; }
set { dock = value; }
}

//Enabled 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。 bool
private bool enabled;

public bool Enabled
{
get { return enabled; }
set { enabled = value; }
}

//Focused 获取一个值,该值指示控件是否有输入焦点。 bool
private bool focused;

public bool Focused
{
get { return focused; }
set { focused = value; }
}

//Font 获取或设置控件显示的文字的字体。 Font
private MyFont myfont;

public MyFont MyFont
{
get { return myfont; }
set { myfont = value; }
}

//ForeColor 获取或设置控件的前景色。 Color
private MyColor foreColor;

public MyColor ForeColor
{
get { return foreColor; }
set { foreColor = value; }
}

//HasChildren 获取一个值,该值指示控件是否包含一个或多个子控件 bool
private bool hasChildren;

public bool HasChildren
{
get { return hasChildren; }
set { hasChildren = value; }
}

//Height 获取或设置控件的高度。 int
private int height;

public int Height
{
get { return height; }
set { height = value; }
}

//Left //获取或设置控件左边缘与其容器的工作区左边缘之间的距离 int
private int left;

public int Left
{
get { return left; }
set { left = value; }
}

//Location 获取或设置该控件的左上角相对于其容器的左上角的坐标。 Point
private Point location;

public Point Location
{
get { return location; }
set { location = value; }
}



//Parent 获取或设置控件的父容器。 ControlModel
//private ControlModel parent;

//public ControlModel Parent
//{
// get { return parent; }
// set { parent = value; }
//}

//Right 获取控件右边缘与其容器的工作区左边缘之间的距离(以像素为单位)。 int
private int right;

public int Right
{
get { return right; }
set { right = value; }
}

//Size 获取或设置控件的高度和宽度。 Size
private Size size;

public Size Size
{
get { return size; }
set { size = value; }
}

//TabIndex 获取或设置在控件的容器的控件的 Tab 键顺序。 int
private int tabIndex;

public int TabIndex
{
get { return tabIndex; }
set { tabIndex = value; }
}

//Text 获取或设置与此控件关联的文本。 string
private string text;

public string Text
{
get { return text; }
set { text = value; }
}



//Top 获取或设置控件上边缘与其容器的工作区上边缘之间的距离(以像素为单位)。 int
private int top;

public int Top
{
get { return top; }
set { top = value; }
}

//Visible 获取或设置一个值,该值指示是否显示该控件。 bool
private bool Visible;

public bool Visible1
{
get { return Visible; }
set { Visible = value; }
}

//Width 获取或设置控件的宽度。 int
private int width;

public int Width
{
get { return width; }
set { width = value; }
}
}


下面是MyFont类,用来保存字体:

[Serializable]
public class MyFont
{
public MyFont(string fontFamily, float size)
{
this.fontFamily = fontFamily;
this.size = size;
}
private string fontFamily;

public string FontFamily
{
get { return fontFamily; }
set { fontFamily = value; }
}
private float size;

public float Size
{
get { return size; }
set { size = value; }
}
}


我在使用
XmlSerializer xs = new XmlSerializer(controlmodel.GetType());
xs.Serialize(sw, controlmodel);
的时候总会出现反射类型错误的异常
而我把MyFont注释掉,就一切正常,也能实现序列化
请高手帮我解答一下啊!
...全文
56 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovefootball 2008-03-24
  • 打赏
  • 举报
回复
[Serializable]
[XmlInclude(typeof(MyFont))] ------->>>>
public class ControlModel
justseven 2008-03-24
  • 打赏
  • 举报
回复
自己顶一下,大家帮忙看看啊

110,539

社区成员

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

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

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