本人设计的一个建模软件中需要对所建模的工程进行持久化保存,请问在c#中如何实现,谢谢!
解决方案 »
- class(get/set) 比较是否相等 望高手解疑
- 将强类型对象动态生成SQL语句
- 对html页面的 postString进行url编码转换无法得到想要的结果
- 如何在不登陆的情况下将WEBPART个性化主页切换到User模式下的某个用户的浏览模式
- 请问怎么给MDI窗体加上一个背景图片
- 怎样调用VB写的DLL亚!!
- 水晶报表中显示多表查询结果可行不? B/S结构,备份服务器的access数据库到一个新access数据库文件可行不?
- C# 如何按profile字段检索
- winform 程序中 sql server定时处理多个表中数据
- 当点击窗体右上角的关闭按钮,窗体关闭,是不是同时也从内存中消除
- 请问哪有免费下载的C#编写的学生成绩管理系统(有原代码的)
- 请教DestroyCursor的用法
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;public class App
{
[STAThread]
static void Main()
{
Serialize();
Deserialize();
} static void Serialize()
{
// Create a hashtable of values that will eventually be serialized.
Hashtable addresses = new Hashtable();
addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301"); // To serialize the hashtable and its key/value pairs,
// you must first open a stream for writing.
// In this case, use a file stream.
FileStream fs = new FileStream("DataFile.dat", FileMode.Create); // Construct a BinaryFormatter and use it to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, addresses);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}
static void Deserialize()
{
// Declare the hashtable reference.
Hashtable addresses = null; // Open the file containing the data that you want to deserialize.
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter(); // Deserialize the hashtable from the file and
// assign the reference to the local variable.
addresses = (Hashtable) formatter.Deserialize(fs);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
} // To prove that the table deserialized correctly,
// display the key/value pairs.
foreach (DictionaryEntry de in addresses)
{
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
}
}
}
告诉你C#自定义对象的序列化:控制序列化的一种简单方式是在对象上实现 ISerializable 接口。但是应注意,在控制序列化时,上一节中的方法优先于此方法。
此外,对于用 Serializable 属性标记且在类级别上或其构造函数上具有声明性或命令性安全的类,不应使用默认序列化。相反,这些类应始终实现 ISerializable 接口。
实现 ISerializable 涉及实现 GetObjectData 方法以及在反序列化对象时使用的特殊构造函数。以下示例代码说明如何对前面节中的 MyObject 类实现 ISerializable。[Serializable]
public class MyObject : ISerializable
{
public int n1;
public int n2;
public String str; public MyObject()
{
} protected MyObject(SerializationInfo info, StreamingContext context)
{
n1 = info.GetInt32("i");
n2 = info.GetInt32("j");
str = info.GetString("k");
}
[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter
=true)]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("i", n1);
info.AddValue("j", n2);
info.AddValue("k", str);
}
}
当序列化期间调用 GetObjectData 时,您负责填充由方法调用提供的 SerializationInfo。只需将要被序列化的变量作为名称/值对添加。任何文本都可用作该名称。您可以自由决定将哪些成员变量添加到 SerializationInfo 中,前提是序列化了足够多的数据,可以在反序列化期间还原该对象。如果基对象实现了 ISerializable,则派生类应对该基对象调用 GetObjectData 方法。
请注意,序列化可使其他代码得以查看或修改本来不可访问的对象实例数据。因此,执行序列化的代码要求指定了 SerializationFormatter 标志的 SecurityPermission。在默认策略下,通过 Internet 下载的代码或 Intranet 代码不会被授予该权限;只有本地计算机上的代码才会被授予该权限。必须对 GetObjectData 方法进行显式保护,方法是要求指定了 SerializationFormatter 标志的 SecurityPermission 或要求其他专门保护私有数据的权限。
如果私有字段中存储了敏感信息,应要求 GetObjectData 具有适当权限,以保护数据。请记住,被授予指定了 SerializationFormatter 标志的 SecurityPermission 的代码可以查看和修改存储在私有字段中的数据。被授予了此 SecurityPermission 的恶意调用方可以查看隐藏目录位置或授予的权限等数据,并使用这些数据攻击计算机的安全漏洞。有关可以指定的安全权限标志的完整列表,请参见 SecurityPermissionFlag 枚举。
有一点必须强调,在向类中添加 ISerializable 时,必须实现 GetObjectData 和特殊构造函数。如果缺少 GetObjectData,编译器将发出警告。但是,由于无法强制实现构造函数,因此如果缺少构造函数,编译器不会发出警告;并且当试图反序列化不带构造函数的类时,会引发异常。
要还原该对象的状态,只需使用在序列化期间使用的名称从 SerializationInfo 检索变量的值即可。如果基类实现 ISerializable,则应调用基构造函数以允许基对象还原其变量。
当您从实现 ISerializable 的类中派生一个新类时,派生类必须同时实现该构造函数以及 GetObjectData 方法(如果派生类具有需要序列化的变量)。下面的代码示例说明如何使用前面所示的 MyObject 类做到这一点[Serializable]
public class ObjectTwo : MyObject
{
public int num; public ObjectTwo() : base()
{
} protected ObjectTwo(SerializationInfo si, StreamingContext context) : base(si,context)
{
num = si.GetInt32("num");
}
[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter
=true)]
public override void GetObjectData(SerializationInfo si, StreamingContext context)
{
base.GetObjectData(si,context);
si.AddValue("num", num);
}
}不要忘记在反序列化构造函数中调用基类。如果没有调用基类,将永远不会调用基类上的构造函数,并且在反序列化后该对象将不会被完全构造。
对象将被彻底重新构造,并且在反序列化期间调用方法可能会产生不良的副作用,因为被调用的方法可能引用在进行此调用时尚未被反序列化的对象引用