ObjectManager ObjectManager ObjectManager ObjectManager Class

定義

在物件還原序列化時保持追蹤。Keeps track of objects as they are deserialized.

public ref class ObjectManager
[System.Runtime.InteropServices.ComVisible(true)]
public class ObjectManager
type ObjectManager = class
Public Class ObjectManager
繼承
ObjectManagerObjectManagerObjectManagerObjectManager
屬性

範例

下列程式碼範例示範如何使用ObjectManager從頭到尾參與所有物件圖形,一次周遊至每個物件的類別。The following code example shows how to use the ObjectManager class to walk through an object graph, traversing to each object only once.

using System;
using System.Text;
using System.Collections;
using System.Runtime.Serialization;
using System.Reflection;

// This class walks through all the objects once in an object graph.
public sealed class ObjectWalker : IEnumerable, IEnumerator {
   private Object m_current;

   // This stack contains the set of objects that will be enumerated.
   private Stack m_toWalk = new Stack();

   // The ObjectIDGenerator ensures that each object is enumerated just once.
   private ObjectIDGenerator m_idGen = new ObjectIDGenerator();

   // Construct an ObjectWalker passing the root of the object graph.
   public ObjectWalker(Object root) {
      Schedule(root);
   }

   // Return an enumerator so this class can be used with foreach.
   public IEnumerator GetEnumerator() {
      return this;
   }

   // Resetting the enumerator is not supported.
   public void Reset() {
      throw new NotSupportedException("Resetting the enumerator is not supported.");
   }

   // Return the enumeration's current object.
   public Object Current { get { return m_current; } }
   
   // Walk the reference of the passed-in object.
   private void Schedule(Object toSchedule) {
      if (toSchedule == null) return;

      // Ask the ObjectIDManager if this object has been examined before.
      Boolean firstOccurrence;
      m_idGen.GetId(toSchedule, out firstOccurrence);

      // If this object has been examined before, do not look at it again just return.
      if (!firstOccurrence) return;

      if (toSchedule.GetType().IsArray) {
         // The object is an array, schedule each element of the array to be looked at.
         foreach (Object item in ((Array)toSchedule)) Schedule(item);
      } else {
         // The object is not an array, schedule this object to be looked at.
         m_toWalk.Push(toSchedule);
      }
   }

   // Advance to the next item in the enumeration.
   public Boolean MoveNext() {
      // If there are no more items to enumerate, return false.
      if (m_toWalk.Count == 0) return false;

      // Check if the object is a terminal object (has no fields that refer to other objects).
      if (!IsTerminalObject(m_current = m_toWalk.Pop())) {
         // The object does have field, schedule the object's instance fields to be enumerated.
         foreach (FieldInfo fi in m_current.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
            Schedule(fi.GetValue(m_current));
         }
      }
      return true;
   }

   // Returns true if the object has no data fields with information of interest.
   private Boolean IsTerminalObject(Object data) {
      Type t = data.GetType();
      return t.IsPrimitive || t.IsEnum || t.IsPointer || data is String;
   }
}


public sealed class App {
   // Define some fields in the class to test the ObjectWalker.
   public String name = "Fred";
   public Int32 Age = 40;

   static void Main() {
      // Build an object graph using an array that refers to various objects.
      Object[] data = new Object[] { "Jeff", 123, 555L, (Byte) 35, new App() };

      // Construct an ObjectWalker and pass it the root of the object graph.
      ObjectWalker ow = new ObjectWalker(data);

      // Enumerate all of the objects in the graph and count the number of objects.
      Int64 num = 0;
      foreach (Object o in ow) {
         // Display each object's type and value as a string.
         Console.WriteLine("Object #{0}: Type={1}, Value's string={2}", 
            num++, o.GetType(), o.ToString());
      }
   }
}

// This code produces the following output.
//
// Object #0: Type=App, Value's string=App
// Object #1: Type=System.Int32, Value's string=40
// Object #2: Type=System.String, Value's string=Fred
// Object #3: Type=System.Byte, Value's string=35
// Object #4: Type=System.Int64, Value's string=555
// Object #5: Type=System.Int32, Value's string=123
// Object #6: Type=System.String, Value's string=Jeff

備註

還原序列化期間,Formatter查詢ObjectManager來判斷是否序列化資料流中的物件參考是指已還原序列化的物件 (回溯參考),或尚未被物件還原序列化 (向前參考)。During deserialization, the Formatter queries the ObjectManager to determine whether a reference to an object in the serialized stream refers to an object that has already been deserialized (a backward reference), or to an object that has not yet been deserialized (a forward reference). 如果序列化資料流的參考是向前參考,則Formatter可以註冊使用修復ObjectManagerIf the reference in the serialized stream is a forward reference, then the Formatter can register a fixup with the ObjectManager. 如果序列化資料流的參考是為了與舊版的參考,Formatter立即完成的參考。If the reference in the serialized stream is a backward reference, the Formatter immediately completes the reference. 修復是指完成在物件還原序列化程序尚未完成的物件參考的程序。Fixup refers to the process of finalizing object references not already completed during the object deserialization process. 已還原序列化的必要的物件後,ObjectManager將完成的參考。After the required object has been deserialized, the ObjectManager will complete the reference.

ObjectManager遵循一組規則,以指定的修復順序。The ObjectManager follows a set of rules that dictate the fixup order. 實作的所有物件ISerializable或具有ISerializationSurrogate可以預期能夠從他們透過傳輸的所有物件SerializationInfo可還原序列化物件樹狀結構時。All objects that implement ISerializable or have a ISerializationSurrogate can expect to have all the objects that they transmitted through SerializationInfo available when the object tree is deserialized. 不過,在父物件不能假設,其所有子物件將會完全都完成完整還原序列化時。However, a parent object cannot presume that all its child objects will be fully completed when it is fully deserialized. 所有子物件將都會出現,但並非所有的孫系物件一定都會存在。All child objects will be present but not all the grandchild objects will necessarily be present. 如果物件需要採取特定動作,取決於它的子物件上執行程式碼,它可以延遲這些動作,實作IDeserializationCallback介面,並只在回呼叫此介面上時,才執行程式碼。If an object needs to take certain actions that depend on executing code on its child objects, it can delay these actions, implement the IDeserializationCallback interface, and execute the code only when it is called back on this interface.

建構函式

ObjectManager(ISurrogateSelector, StreamingContext) ObjectManager(ISurrogateSelector, StreamingContext) ObjectManager(ISurrogateSelector, StreamingContext) ObjectManager(ISurrogateSelector, StreamingContext)

初始化 ObjectManager 類別的新執行個體。Initializes a new instance of the ObjectManager class.

方法

DoFixups() DoFixups() DoFixups() DoFixups()

執行所有記錄的修復。Performs all the recorded fixups.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetObject(Int64) GetObject(Int64) GetObject(Int64) GetObject(Int64)

傳回具有所指定物件 ID 的物件。Returns the object with the specified object ID.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
RaiseDeserializationEvent() RaiseDeserializationEvent() RaiseDeserializationEvent() RaiseDeserializationEvent()

引發還原序列化事件至任何實作 IDeserializationCallback 的已登錄物件。Raises the deserialization event to any registered object that implements IDeserializationCallback.

RaiseOnDeserializingEvent(Object) RaiseOnDeserializingEvent(Object) RaiseOnDeserializingEvent(Object) RaiseOnDeserializingEvent(Object)

叫用 (Invoke) 以 OnDeserializingAttribute 標記的方法。Invokes the method marked with the OnDeserializingAttribute.

RecordArrayElementFixup(Int64, Int32, Int64) RecordArrayElementFixup(Int64, Int32, Int64) RecordArrayElementFixup(Int64, Int32, Int64) RecordArrayElementFixup(Int64, Int32, Int64)

記錄陣列中單一元素的修復。Records a fixup for one element in an array.

RecordArrayElementFixup(Int64, Int32[], Int64) RecordArrayElementFixup(Int64, Int32[], Int64) RecordArrayElementFixup(Int64, Int32[], Int64) RecordArrayElementFixup(Int64, Int32[], Int64)

記錄陣列中指定之元素的修復,以便稍後執行。Records fixups for the specified elements in an array, to be executed later.

RecordDelayedFixup(Int64, String, Int64) RecordDelayedFixup(Int64, String, Int64) RecordDelayedFixup(Int64, String, Int64) RecordDelayedFixup(Int64, String, Int64)

記錄物件成員的修復,以便稍後執行。Records a fixup for an object member, to be executed later.

RecordFixup(Int64, MemberInfo, Int64) RecordFixup(Int64, MemberInfo, Int64) RecordFixup(Int64, MemberInfo, Int64) RecordFixup(Int64, MemberInfo, Int64)

記錄物件成員的修復,以便稍後執行。Records a fixup for a member of an object, to be executed later.

RegisterObject(Object, Int64) RegisterObject(Object, Int64) RegisterObject(Object, Int64) RegisterObject(Object, Int64)

在物件還原序列化時登錄該物件,將它與 objectID 相關聯。Registers an object as it is deserialized, associating it with objectID.

RegisterObject(Object, Int64, SerializationInfo) RegisterObject(Object, Int64, SerializationInfo) RegisterObject(Object, Int64, SerializationInfo) RegisterObject(Object, Int64, SerializationInfo)

在物件還原序列化時登錄該物件,將它與 objectID 相關聯,並記錄和它一起使用的 SerializationInfoRegisters an object as it is deserialized, associating it with objectID, and recording the SerializationInfo used with it.

RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo) RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo) RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo) RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo)

在物件還原序列化時登錄該物件,將它與 objectID 相關聯,並記錄 SerializationInfoRegisters a member of an object as it is deserialized, associating it with objectID, and recording the SerializationInfo.

RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo, Int32[]) RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo, Int32[]) RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo, Int32[]) RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo, Int32[])

當物件還原序列化時,登錄物件中所包含的陣列成員,將它與 objectID 相關聯,並記錄 SerializationInfoRegisters a member of an array contained in an object while it is deserialized, associating it with objectID, and recording the SerializationInfo.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於