此文章由机器翻译。 将光标移到文章的句子上,以查看原文。 更多信息。
译文
原文
此主题尚未评级 - 评价此主题

SerializableAttribute 类

指示一个类可以序列化。 此类不能被继承。

System.Object
  System.Attribute
    System.SerializableAttribute

命名空间:  System
程序集:  mscorlib(在 mscorlib.dll 中)
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Delegate, Inherited = false)]
[ComVisibleAttribute(true)]
public sealed class SerializableAttribute : Attribute

SerializableAttribute 类型公开以下成员。

  名称说明
公共方法由 XNA Framework 提供支持SerializableAttribute初始化 SerializableAttribute 类的新实例。
页首
  名称说明
公共属性TypeId当在派生类中实现时,获取该 Attribute 的唯一标识符。 (继承自 Attribute。)
页首
  名称说明
公共方法由 XNA Framework 提供支持Equals基础结构。返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute。)
公共方法由 XNA Framework 提供支持GetHashCode返回此实例的哈希代码。 (继承自 Attribute。)
公共方法由 XNA Framework 提供支持GetType获取当前实例的 Type (继承自 Object。)
公共方法IsDefaultAttribute当在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute。)
公共方法由 XNA Framework 提供支持Match当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute。)
公共方法由 XNA Framework 提供支持ToString返回表示当前对象的字符串。 (继承自 Object。)
页首
  名称说明
显式接口实现私有方法_Attribute.GetIDsOfNames将一组名称映射为对应的一组调度标识符。 (继承自 Attribute。)
显式接口实现私有方法_Attribute.GetTypeInfo检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute。)
显式接口实现私有方法_Attribute.GetTypeInfoCount检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute。)
显式接口实现私有方法_Attribute.Invoke提供对某一对象公开的属性和方法的访问。 (继承自 Attribute。)
页首

SerializableAttribute 特性应用于一个类型可指示该类型的实例可以序列化。 如果正在序列化的对象图中的任何类型未应用 SerializableAttribute 特性,公共语言运行时则会引发 SerializationException

即使该类也会实现 ISerializable 接口来控制序列化进程,仍要应用 SerializableAttribute 特性。

SerializableAttribute 特性应用于类型时,序列化默认情况下所有私有和公共字段。 您可以通过实现接口 ISerializable 重写序列化控件序列化 granularly 处理。

也可以从序列化排除字段通过应用 NonSerializedAttribute 特性应用于字段。 如果可序列化类型的字段包含指针、句柄或其他某些针对于特定环境的数据结构,并且不能在不同的环境中以有意义的方式重建,则最好将 NonSerializedAttribute 特性应用于该字段。

有关使用属性的更多信息,请参见利用特性扩展元数据 有关序列化的更多信息,请参见 System.Runtime.Serialization

下面的示例演示用 SerializableAttribute 特性标记的一个对象的序列化。 若要使用 BinaryFormatter 而不是 SoapFormatter,请取消注释相应的行。


using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
//using System.Runtime.Serialization.Formatters.Binary;

public class Test {
   public static void Main()  {

      //Creates a new TestSimpleObject object.
      TestSimpleObject obj = new TestSimpleObject();

      Console.WriteLine("Before serialization the object contains: ");
      obj.Print();

      //Opens a file and serializes the object into it in binary format.
      Stream stream = File.Open("data.xml", FileMode.Create);
      SoapFormatter formatter = new SoapFormatter();

      //BinaryFormatter formatter = new BinaryFormatter();

      formatter.Serialize(stream, obj);
      stream.Close();

      //Empties obj.
      obj = null;

      //Opens file "data.xml" and deserializes the object from it.
      stream = File.Open("data.xml", FileMode.Open);
      formatter = new SoapFormatter();

      //formatter = new BinaryFormatter();

      obj = (TestSimpleObject)formatter.Deserialize(stream);
      stream.Close();

      Console.WriteLine("");
      Console.WriteLine("After deserialization the object contains: ");
      obj.Print();
   }
}


// A test object that needs to be serialized.
[Serializable()]		
public class TestSimpleObject  {

    public int member1;
    public string member2;
    public string member3;
    public double member4;

    // A field that is not serialized.
    [NonSerialized()] public string member5; 

    public TestSimpleObject() {

        member1 = 11;
        member2 = "hello";
        member3 = "hello";
        member4 = 3.14159265;
        member5 = "hello world!";
    }


    public void Print() {

        Console.WriteLine("member1 = '{0}'", member1);
        Console.WriteLine("member2 = '{0}'", member2);
        Console.WriteLine("member3 = '{0}'", member3);
        Console.WriteLine("member4 = '{0}'", member4);
        Console.WriteLine("member5 = '{0}'", member5);
    }
}


.NET Framework

受以下版本支持:4.5、4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
本文是否对您有所帮助?
(1500 个剩余字符)

社区附加资源

添加
© 2013 Microsoft. 版权所有。