持久化的问题是一个很古老的问题,看似和我们的日常应用没有什么联系,但是一些情况下还是很有用处的。
场景描述:
应用场景介绍:

CS下 (Windows程序)

BS下(Web页面)
做原型时候,没有确定BS和CS结构的情况下,需要构建一个简单的模型,使得你构建的简单界面既能支持cs又能支持BS。我们的目的是在cs情况下编辑,然后在BS下生成编辑好的页面。
简单实现:
下面只提供整个应用场景的拙劣实现,单单为了说明序列化和反序列化的问题:
将WebMatrixServer的两个Dll(Microsoft.Matrix.dll,Microsoft.Matrix.WebHost.dll)引入工程,使程序自己变成可以显示页面的Web服务器(相当于IIS)。用WebBrowser控件来展示页面。
以Button为例子,在cs模式下将button序列化为二进制文件。在asp.net下将序列化的文件反序列化出来。获取编辑控件的信息。
public interface ISoildControl

{

Point ControlPos
{ get; set; }

bool EditState
{ get; set; }
}
ISolidControl的目的是控制所有实现ISolidContorl接口的控件,并判别是否为编辑状态,所有控件在编辑状态下是可以拖动的。 这与本实例无关,仅作辅助说明。
让类支持序列化和反序列化:
[Serializable]
public class SolidButton : Button, ISoildControl, ISerializable

{
private Point CurPos;
private bool MoveFlag = false;
private bool isEdit = false;

public SolidButton(SerializationInfo info, StreamingContext ctxt)

{
this.Location = new Point((int)info.GetValue("X", typeof(int)),(int)info.GetValue("Y", typeof(int)));
this.Text = (String)info.GetValue("Text", typeof(string));
this.Height = (int)info.GetValue("Height", typeof(int));
this.Width = (int)info.GetValue("Width", typeof(int));
}

public SolidButton()

{
this.MouseDown +=new MouseEventHandler(ButtonMouseDown);
this.MouseMove +=new MouseEventHandler(ButtonMouseMove);
this.MouseUp +=new MouseEventHandler(ButtonMouseUp);
}

private void ButtonMouseDown(object sender, MouseEventArgs e)

{

MoveFlag = true;
CurPos = new Point(e.X, e.Y);
}
private void ButtonMouseMove(object sender, MouseEventArgs e)

{

if (MoveFlag && isEdit)

{
int PosX, PosY;
PosX = CurPos.X - e.X;
PosY = CurPos.Y - e.Y;
this.Location = new Point(this.Location.X - PosX, this.Location.Y - PosY);
}
}
private void ButtonMouseUp(object sender, MouseEventArgs e)

{
MoveFlag = false;
}
public Point ControlPos

{

get
{ return this.Location; }

set
{ this.Location = value; }
}
public bool EditState

{

get
{ return isEdit;}

set
{ isEdit = value; }
}



ISerializable Members#region ISerializable Members
[SecurityPermission(SecurityAction.LinkDemand,Flags = SecurityPermissionFlag.SerializationFormatter)]
public void GetObjectData(SerializationInfo info, StreamingContext context)

{

info.AddValue("Text", this.Text);
info.AddValue("X", this.Location.X);
info.AddValue("Y", this.Location.Y);
info.AddValue("Height", this.Height);
info.AddValue("Width",this.Width);
}

#endregion

}
1.单单在类上声明
[Serializable],不能将SolideButton序列化,提示他的父类不能被序列化。于是实现
ISerializable接口。
2. 如果不声明
public SolidButton(SerializationInfo info, StreamingContext ctxt) 的构造函数,就会报出没有反序列化构造函数的错误。 一定要记住,如果支持反序列化,必须实现这一类型的反序列化构造函数。
实际应用:
应用场景:
CS下:
Stream stream = File.OpenWrite(@"c:\prep.bin");
try

{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, button);
stream.Close();
}
catch (Exception ex)

{
stream.Close();
}
BS下:
FileStream stream = new FileStream(@"c:\\prep.bin", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
SolidButton sd = bf.Deserialize(stream) as SolidButton;
this.Button1.Text = sd.Text;
this.Button1.Style.Add("top", sd.Location.Y.ToString()+"px");
this.Button1.Style.Add("left", sd.Location.X.ToString() + "px");
this.Button1.Style.Add("width",sd.Width.ToString()+"px");
this.Button1.Style.Add("height", sd.Height.ToString() + "px");


stream.Close();
其中Button1为asp.net中的Button控件。
posted on 2008-04-08 14:56
最远距离 阅读(3172)
评论(10) 编辑 收藏 所属分类:
Asp.net