閱讀英文

共用方式為


NonSerializedAttribute 類別

定義

指示可序列化類別的欄位不應該序列化。 此類別無法獲得繼承。

C#
[System.AttributeUsage(System.AttributeTargets.Field, Inherited=false)]
public sealed class NonSerializedAttribute : Attribute
C#
[System.AttributeUsage(System.AttributeTargets.Field, Inherited=true)]
public sealed class NonSerializedAttribute : Attribute
C#
[System.AttributeUsage(System.AttributeTargets.Field, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class NonSerializedAttribute : Attribute
繼承
NonSerializedAttribute
屬性

範例

下列範例示範使用 屬性標記 SerializableAttribute 的物件序列化,以及序列化物件中以 標記 NonSerializedAttribute 之 欄位的行為。

注意

程式碼會 SoapFormatter 使用 類別來序列化 物件。 類別位於system.runtime.serialization.formatters.soap.dll中,預設不會載入專案中。 若要執行程式碼,您必須將 DLL 的參考新增至專案。

C#
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);
    }
}

備註

使用 BinaryFormatterSoapFormatter 類別序列化物件時,請使用 NonSerializedAttribute 屬性防止欄位序列化。 例如,您可以使用這個屬性來防止敏感性資料的序列化。

屬性的目標物件 NonSerializedAttribute 是可序列化類別的公用和私用欄位。 根據預設,除非類別標示 SerializableAttribute 為 ,否則類別不可序列化。 在序列化過程中,類別的所有公用和私用欄位預設都會序列化。 標示為 NonSerializedAttribute 的欄位會在序列化期間排除。 如果您使用 XmlSerializer 類別來序列化物件,請使用 XmlIgnoreAttribute 類別來取得相同的功能。 或者,實作 ISerializable 介面,以明確控制序列化程式。 請注意,實 ISerializable 作的類別仍必須標示為 SerializableAttribute

若要將 NonSerializedAttribute 類別套用至事件,請將屬性位置設定為 欄位,如下列 C# 程式碼所示。

C#
[field:NonSerializedAttribute()]   
public event ChangedEventHandler Changed;  

如果欄位未序列化,但仍需要還原序列化之後必須提供的預設值,您可以建立提供具有值之欄位的方法,然後將 套用 OnDeserializedAttribute 至 方法。

如需使用屬性的詳細資訊,請參閱 屬性

建構函式

NonSerializedAttribute()

初始化 NonSerializedAttribute 類別的新執行個體。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

另請參閱