The Labs \ Source Viewer \ SSCLI \ System.Runtime.Serialization \ SerializationInfoEnumerator

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class: SerializationInfoEnumerator
  18. **
  19. **
  20. ** Purpose: A formatter-friendly mechanism for walking all of
  21. ** the data in a SerializationInfo.  Follows the IEnumerator
  22. ** mechanism from Collections.
  23. **
  24. **
  25. ============================================================*/
  26. namespace System.Runtime.Serialization
  27. {
  28.     using System;
  29.     using System.Collections;
  30.    
  31.     //
  32.     // The tuple returned by SerializationInfoEnumerator.Current.
  33.     //
  34.     [System.Runtime.InteropServices.ComVisible(true)]
  35.     public struct SerializationEntry
  36.     {
  37.         private Type m_type;
  38.         private object m_value;
  39.         private string m_name;
  40.        
  41.         public object Value {
  42.             get { return m_value; }
  43.         }
  44.        
  45.         public string Name {
  46.             get { return m_name; }
  47.         }
  48.        
  49.         public Type ObjectType {
  50.             get { return m_type; }
  51.         }
  52.        
  53.         internal SerializationEntry(string entryName, object entryValue, Type entryType)
  54.         {
  55.             m_value = entryValue;
  56.             m_name = entryName;
  57.             m_type = entryType;
  58.         }
  59.     }
  60.    
  61.     //
  62.     // A simple enumerator over the values stored in the SerializationInfo.
  63.     // This does not snapshot the values, it just keeps pointers to the
  64.     // member variables of the SerializationInfo that created it.
  65.     //
  66.     [System.Runtime.InteropServices.ComVisible(true)]
  67.     public sealed class SerializationInfoEnumerator : IEnumerator
  68.     {
  69.         string[] m_members;
  70.         object[] m_data;
  71.         Type[] m_types;
  72.         int m_numItems;
  73.         int m_currItem;
  74.         bool m_current;
  75.        
  76.         internal SerializationInfoEnumerator(string[] members, object[] info, Type[] types, int numItems)
  77.         {
  78.             BCLDebug.Assert(members != null, "[SerializationInfoEnumerator.ctor]members!=null");
  79.             BCLDebug.Assert(info != null, "[SerializationInfoEnumerator.ctor]info!=null");
  80.             BCLDebug.Assert(types != null, "[SerializationInfoEnumerator.ctor]types!=null");
  81.             BCLDebug.Assert(numItems >= 0, "[SerializationInfoEnumerator.ctor]numItems>=0");
  82.             BCLDebug.Assert(members.Length >= numItems, "[SerializationInfoEnumerator.ctor]members.Length>=numItems");
  83.             BCLDebug.Assert(info.Length >= numItems, "[SerializationInfoEnumerator.ctor]info.Length>=numItems");
  84.             BCLDebug.Assert(types.Length >= numItems, "[SerializationInfoEnumerator.ctor]types.Length>=numItems");
  85.            
  86.             m_members = members;
  87.             m_data = info;
  88.             m_types = types;
  89.             //The MoveNext semantic is much easier if we enforce that [0..m_numItems] are valid entries
  90.             //in the enumerator, hence we subtract 1.
  91.             m_numItems = numItems - 1;
  92.             m_currItem = -1;
  93.             m_current = false;
  94.         }
  95.        
  96.         public bool MoveNext()
  97.         {
  98.             if (m_currItem < m_numItems) {
  99.                 m_currItem++;
  100.                 m_current = true;
  101.             }
  102.             else {
  103.                 m_current = false;
  104.             }
  105.             return m_current;
  106.         }
  107.        
  108.         /// <internalonly/>
  109.         object IEnumerator.Current {
  110.             //Actually returns a SerializationEntry
  111.             get {
  112.                 if (m_current == false) {
  113.                     throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
  114.                 }
  115.                 return (object)(new SerializationEntry(m_members[m_currItem], m_data[m_currItem], m_types[m_currItem]));
  116.             }
  117.         }
  118.        
  119.         public SerializationEntry Current {
  120.             //Actually returns a SerializationEntry
  121.             get {
  122.                 if (m_current == false) {
  123.                     throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
  124.                 }
  125.                 return (new SerializationEntry(m_members[m_currItem], m_data[m_currItem], m_types[m_currItem]));
  126.             }
  127.         }
  128.        
  129.         public void Reset()
  130.         {
  131.             m_currItem = -1;
  132.             m_current = false;
  133.         }
  134.        
  135.         public string Name {
  136.             get {
  137.                 if (m_current == false) {
  138.                     throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
  139.                 }
  140.                 return m_members[m_currItem];
  141.             }
  142.         }
  143.         public object Value {
  144.             get {
  145.                 if (m_current == false) {
  146.                     throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
  147.                 }
  148.                 return m_data[m_currItem];
  149.             }
  150.         }
  151.         public Type ObjectType {
  152.             get {
  153.                 if (m_current == false) {
  154.                     throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumOpCantHappen"));
  155.                 }
  156.                 return m_types[m_currItem];
  157.             }
  158.         }
  159.     }
  160. }

Developer Fusion