ObjectManager Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Effectue le suivi des objets au fur et à mesure qu’ils sont désérialisés.
public ref class ObjectManager
public class ObjectManager
[System.Runtime.InteropServices.ComVisible(true)]
public class ObjectManager
type ObjectManager = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type ObjectManager = class
Public Class ObjectManager
- Héritage
-
ObjectManager
- Attributs
Exemples
L’exemple de code suivant montre comment utiliser la ObjectManager classe pour parcourir un graphique d’objet, en parcourant chaque objet une seule fois.
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
Remarques
Lors de la désérialisation, les Formatter requêtes permettent ObjectManager de déterminer si une référence à un objet dans le flux sérialisé fait référence à un objet qui a déjà été désérialisé (référence descendante) ou à un objet qui n’a pas encore été désérialisé (référence vers l’avant). Si la référence dans le flux sérialisé est une référence vers l’avant, elle Formatter peut inscrire un correctif auprès du ObjectManager. Si la référence dans le flux sérialisé est une référence descendante, elle Formatter termine immédiatement la référence. Fixup fait référence au processus de finalisation des références d’objet non déjà terminées pendant le processus de désérialisation de l’objet. Une fois l’objet requis désérialisé, la ObjectManager référence est terminée.
Voici ObjectManager un ensemble de règles qui dictent l’ordre de correction. Tous les objets qui implémentent ISerializable ou ont un ISerializationSurrogate peut s’attendre à avoir tous les objets qu’ils ont transmis via SerializationInfo disponibles lorsque l’arborescence d’objets est désérialisée. Toutefois, un objet parent ne peut pas présumer que tous ses objets enfants seront entièrement terminés lorsqu’il est entièrement désérialisé. Tous les objets enfants seront présents, mais pas tous les objets petits-enfants seront nécessairement présents. Si un objet doit effectuer certaines actions qui dépendent de l’exécution du code sur ses objets enfants, il peut retarder ces actions, implémenter l’interface et exécuter le IDeserializationCallback code uniquement lorsqu’il est rappelé sur cette interface.
Constructeurs
| Nom | Description |
|---|---|
| ObjectManager(ISurrogateSelector, StreamingContext) |
Initialise une nouvelle instance de la classe ObjectManager. |
Méthodes
| Nom | Description |
|---|---|
| DoFixups() |
Effectue toutes les corrections enregistrées. |
| Equals(Object) |
Détermine si l’objet spécifié est égal à l’objet actuel. (Hérité de Object) |
| GetHashCode() |
Sert de fonction de hachage par défaut. (Hérité de Object) |
| GetObject(Int64) |
Retourne l’objet avec l’ID d’objet spécifié. |
| GetType() |
Obtient la Type de l’instance actuelle. (Hérité de Object) |
| MemberwiseClone() |
Crée une copie superficielle du Objectactuel. (Hérité de Object) |
| RaiseDeserializationEvent() |
Déclenche l’événement de désérialisation sur n’importe quel objet inscrit qui implémente IDeserializationCallback. |
| RaiseOnDeserializingEvent(Object) |
Appelle la méthode marquée avec le OnDeserializingAttribute. |
| RecordArrayElementFixup(Int64, Int32, Int64) |
Enregistre une correction pour un élément d’un tableau. |
| RecordArrayElementFixup(Int64, Int32[], Int64) |
Enregistre les correctifs pour les éléments spécifiés dans un tableau, à exécuter ultérieurement. |
| RecordDelayedFixup(Int64, String, Int64) |
Enregistre un correctif pour un membre d’objet, à exécuter ultérieurement. |
| RecordFixup(Int64, MemberInfo, Int64) |
Enregistre un correctif pour un membre d’un objet, à exécuter ultérieurement. |
| RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo, Int32[]) |
Inscrit un membre d’un tableau contenu dans un objet alors qu’il est désérialisé, l’associant |
| RegisterObject(Object, Int64, SerializationInfo, Int64, MemberInfo) |
Inscrit un membre d’un objet tel qu’il est désérialisé, l’associant |
| RegisterObject(Object, Int64, SerializationInfo) |
Inscrit un objet tel qu’il est désérialisé, l’associant |
| RegisterObject(Object, Int64) |
Inscrit un objet tel qu’il est désérialisé, l’associant à |
| ToString() |
Retourne une chaîne qui représente l’objet actuel. (Hérité de Object) |