本人设计的一个建模软件中需要对所建模的工程进行持久化保存,请问在c#中如何实现,谢谢!

解决方案 »

  1.   

    C# 本身提供了持久化支持,不需要数据库.直接就能将Object保存到文件中.默认的是二进制形式,可以选择XML形式.
      

  2.   

    http://chs.gotdotnet.com/quickstart/util/srcview.aspx?path=%2fquickstart%2fhowto%2fsamples%2fserialization%2fserialize%2fserialize.src
      

  3.   

    using System;
    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);
            }
        }
    }
      

  4.   

    楼主:
       告诉你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,编译器将发出警告。但是,由于无法强制实现构造函数,因此如果缺少构造函数,编译器不会发出警告;并且当试图反序列化不带构造函数的类时,会引发异常。
      

  5.   

    在反序列化期间,使用为此目的提供的构造函数将 SerializationInfo 传递给类。在反序列化对象时,对构造函数施加的任何可见性约束均被忽略,因此可以将类标记为 public、protected、internal 或 private。但是,如果类未被密封,则最好将构造函数标记为 protected;如果类被密封了,则应将构造函数标记为 private。构造函数还应执行彻底的输入验证。为了避免恶意代码的滥用,构造函数应强制使用任何其他构造函数获取类实例所需的同样的安全检查和权限。如果不采用此建议,恶意代码将可以预先序列化对象,通过指定了 SerializationFormatter 标志的 SecurityPermission 获得控制权,然后在客户端计算机上反序列化对象,避开本来在标准实例构造过程中会使用公共构造函数应用的任何安全性。
    要还原该对象的状态,只需使用在序列化期间使用的名称从 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);
      }
    }不要忘记在反序列化构造函数中调用基类。如果没有调用基类,将永远不会调用基类上的构造函数,并且在反序列化后该对象将不会被完全构造。
    对象将被彻底重新构造,并且在反序列化期间调用方法可能会产生不良的副作用,因为被调用的方法可能引用在进行此调用时尚未被反序列化的对象引用