ObjectManager クラスとは? わかりやすく解説

ObjectManager クラス

シリアル化されたオブジェクト追跡します

名前空間: System.Runtime.Serialization
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Class ObjectManager
[ComVisibleAttribute(true)] 
public class ObjectManager
[ComVisibleAttribute(true)] 
public ref class ObjectManager
/** @attribute ComVisibleAttribute(true) */ 
public class ObjectManager
ComVisibleAttribute(true) 
public class ObjectManager
解説解説

シリアル化中にFormatterObjectManager照会してシリアル化ストリームオブジェクトへの参照が逆シリアル化済みオブジェクト参照しているのか (後方参照)、またはまだ逆シリアル化されていないオブジェクト参照しているのか (前方参照) どうかを判断しますシリアル化ストリーム参照前方参照である場合は、FormatterObjectManager によってフィックスアップを登録しますシリアル化ストリーム参照後方参照である場合は、Formatter がすぐにその参照完了します。フィックスアップとは、オブジェクトの逆シリアル化処理で、まだ完了していないオブジェクト参照完了する処理を指します要求したオブジェクトが逆シリアル化された後でObjectManager がその参照完了します

ObjectManager は、フィックスアップ順序指示する一連の規則従います。ISerializable を実装しているか、または ISerializationSurrogate を持っているすべてのオブジェクトは、オブジェクト ツリーが逆シリアル化されるときに使用できる SerializationInfo を使用して送信したオブジェクトをすべて持つことができます。ただし、親オブジェクトは、自らが完全に逆シリアル化されたときに、そのすべてのオブジェクト完成されているとは仮定できません。すべてのオブジェクト存在しますが、すべてのオブジェクト存在するとは限りません。オブジェクトその子オブジェクトでのコード実行依存するなんらかのアクション実行する必要がある場合は、このアクション延期し、IDeserializationCallback インターフェイス実装して、このインターフェイスコール バックされたときにだけコード実行できます

使用例使用例

ObjectManager クラス使用して、各オブジェクト一度だけ走査しながら、オブジェクト グラフを順をおって調べていく方法次のコード例示します

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
継承階層継承階層
System.Object
  System.Runtime.Serialization.ObjectManager
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ObjectManager メンバ
System.Runtime.Serialization 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ObjectManager クラス」の関連用語

ObjectManager クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ObjectManager クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS