本文為機器翻譯文章。如需檢視英文版,請選取 [原文] 核取方塊。您也可以將滑鼠指標移到文字上,即可在快顯視窗顯示英文原文。
譯文
原文

OnDeserializedAttribute 類別

 

套用至方法時,指定在還原序列化物件圖形中的物件後立即呼叫該方法。 還原序列化相對於圖形中其他物件的順序不具決定性。

命名空間:   System.Runtime.Serialization
組件:  mscorlib (於 mscorlib.dll)

System.Object
  System.Attribute
    System.Runtime.Serialization.OnDeserializedAttribute

[AttributeUsageAttribute(AttributeTargets.Method, Inherited = false)]
[ComVisibleAttribute(true)]
public sealed class OnDeserializedAttribute : Attribute

名稱描述
System_CAPS_pubmethodOnDeserializedAttribute()

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

名稱描述
System_CAPS_pubpropertyTypeId

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

名稱描述
System_CAPS_pubmethodEquals(Object)

此 API 支援 產品 基礎結構,而且不適合直接從程式碼使用。 傳回值,這個值指出此執行個體是否與指定的物件相等。(繼承自 Attribute。)

System_CAPS_pubmethodGetHashCode()

傳回這個執行個體的雜湊碼。(繼承自 Attribute。)

System_CAPS_pubmethodGetType()

取得目前執行個體的 Type(繼承自 Object。)

System_CAPS_pubmethodIsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。(繼承自 Attribute。)

System_CAPS_pubmethodMatch(Object)

當在衍生類別中覆寫時,傳回值,指出這個執行個體是否等於指定的物件。(繼承自 Attribute。)

System_CAPS_pubmethodToString()

傳回代表目前物件的字串。(繼承自 Object。)

名稱描述
System_CAPS_pubinterfaceSystem_CAPS_privmethod_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

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

System_CAPS_pubinterfaceSystem_CAPS_privmethod_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。(繼承自 Attribute。)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_Attribute.GetTypeInfoCount(UInt32)

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

System_CAPS_pubinterfaceSystem_CAPS_privmethod_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。(繼承自 Attribute。)

使用 OnDeserializedAttribute 當您需要修正已還原序列化的物件上的值,已還原序列化之後,圖形會在傳回之前。 可以使用這個屬性,而不是 IDeserializationCallback 介面。

若要使用 OnDeserializedAttribute, ,此方法必須包含 StreamingContext 參數。 屬性標記會由序列化基礎結構所呼叫的方法和 StreamingContext 提供其他相關型別進行序列化的資料。 使用下列程式碼所示︰

[OnDeserialized]
private void SetValuesOnDeserialized(StreamingContext context)
{
    // Code not shown.
}
System_CAPS_note注意

在程式碼中,您可以使用字組 OnDeserialized 來代替較長的 OnDeserializedAttribute

下列範例會套用 OnDeserializedAttribute, ,OnSerializingAttribute, ,OnSerializedAttribute, ,和 OnDeserializingAttribute 屬性至類別的方法。

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

public class Test
{
    public static void Main()
    {
        // Create a new TestSimpleObject object.
        TestSimpleObject obj = new TestSimpleObject();

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

        // Open a file and serialize the object into binary format.
        Stream stream = File.Open("DataFile.dat", FileMode.Create);
        BinaryFormatter formatter = new BinaryFormatter();

        try
        {
            formatter.Serialize(stream, obj);

            // Print the object again to see the effect of the 
            //OnSerializedAttribute.
            Console.WriteLine("\n After serialization the object contains: ");
            obj.Print();

            // Set the original variable to null.
            obj = null;
            stream.Close();

            // Open the file "DataFile.dat" and deserialize the object from it.
            stream = File.Open("DataFile.dat", FileMode.Open);

            // Deserialize the object from the data file.
            obj = (TestSimpleObject)formatter.Deserialize(stream);

            Console.WriteLine("\n After deserialization the object contains: ");
            obj.Print();
            Console.ReadLine();
        }
        catch (SerializationException se)
        {
            Console.WriteLine("Failed to serialize. Reason: " + se.Message);
            throw;
        }
        catch (Exception exc)
        {
            Console.WriteLine("An exception occurred. Reason: " + exc.Message);
            throw;
        }
        finally
        {
            stream.Close();
            obj = null;
            formatter = null;
        }

    }
}


// This is the object that will be serialized and deserialized.
[Serializable()]
public class TestSimpleObject
{
    // This member is serialized and deserialized with no change.
    public int member1;

    // The value of this field is set and reset during and 
    // after serialization.
    private string member2;

    // This field is not serialized. The OnDeserializedAttribute 
    // is used to set the member value after serialization.
    [NonSerialized()]
    public string member3;

    // This field is set to null, but populated after deserialization.
    private string member4;

    // Constructor for the class.
    public TestSimpleObject()
    {
        member1 = 11;
        member2 = "Hello World!";
        member3 = "This is a nonserialized value";
        member4 = null;
    }

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

    [OnSerializing()]
    internal void OnSerializingMethod(StreamingContext context)
    {
        member2 = "This value went into the data file during serialization.";
    }

    [OnSerialized()]
    internal void OnSerializedMethod(StreamingContext context)
    {
        member2 = "This value was reset after serialization.";
    }

    [OnDeserializing()]
    internal void OnDeserializingMethod(StreamingContext context)
    {
        member3 = "This value was set during deserialization";
    }

    [OnDeserialized()]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        member4 = "This value was set after deserialization.";
    }
}

// Output:
//  Before serialization the object contains: 
// member1 = '11'
// member2 = 'Hello World!'
// member3 = 'This is a nonserialized value'
// member4 = ''
//
//  After serialization the object contains: 
// member1 = '11'
// member2 = 'This value was reset after serialization.'
// member3 = 'This is a nonserialized value'
// member4 = ''
//
//  After deserialization the object contains: 
// member1 = '11'
// member2 = 'This value went into the data file during serialization.'
// member3 = 'This value was set during deserialization'
// member4 = 'This value was set after deserialization.'

通用 Windows 平台
自 8 起供應
.NET Framework
自 2.0 起供應
可攜式類別庫
提供支援︰ 可攜式 .NET 平台
Silverlight
自 3.0 起供應
Windows Phone Silverlight
自 7.0 起供應
Windows Phone
自 8.1 起供應

此類型的任何 public static (在 Visual Basic 中 共用 在 Visual Basic 中) 成員皆為安全執行緒。不保證任何執行個體成員為安全執行緒。

回到頁首
顯示: