The Labs \ Source Viewer \ SSCLI \ Microsoft.JScript \ ScriptObjectPropertyEnumerator

  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. namespace Microsoft.JScript
  16. {
  17.    
  18.     using System;
  19.     using System.Collections;
  20.     using System.Reflection;
  21.    
  22.     internal class ScriptObjectPropertyEnumerator : IEnumerator
  23.     {
  24.         private ArrayList enumerators;
  25.         private ArrayList objects;
  26.         private int index;
  27.         private SimpleHashtable visited_names;
  28.        
  29.         internal ScriptObjectPropertyEnumerator(ScriptObject obj)
  30.         {
  31.             obj.GetPropertyEnumerator(this.enumerators = new ArrayList(), this.objects = new ArrayList());
  32.             this.index = 0;
  33.             this.visited_names = new SimpleHashtable(16);
  34.         }
  35.        
  36.         public virtual bool MoveNext()
  37.         {
  38.             if (this.index >= this.enumerators.Count)
  39.                 return false;
  40.             IEnumerator ienum = (IEnumerator)this.enumerators[this.index];
  41.             bool result = ienum.MoveNext();
  42.             if (!result) {
  43.                 this.index++;
  44.                 return this.MoveNext();
  45.             }
  46.             object ob = ienum.Current;
  47.             string pname;
  48.             FieldInfo field = ob as FieldInfo;
  49.             if (field != null) {
  50.                 JSPrototypeField pfield = ob as JSPrototypeField;
  51.                 if (pfield != null && pfield.value is Missing)
  52.                     return this.MoveNext();
  53.                 pname = field.Name;
  54.                 object val = field.GetValue(this.objects[this.index]);
  55.                 if (val is Missing)
  56.                     return this.MoveNext();
  57.             }
  58.             else if (ob is string)
  59.                 pname = (string)ob;
  60.             else if (ob is MemberInfo)
  61.                 pname = ((MemberInfo)ob).Name;
  62.             else
  63.                 pname = ob.ToString();
  64.             if (this.visited_names[pname] != null)
  65.                 return this.MoveNext();
  66.             this.visited_names[pname] = pname;
  67.             return true;
  68.         }
  69.        
  70.         public virtual object Current {
  71.             get {
  72.                 object ob = ((IEnumerator)this.enumerators[this.index]).Current;
  73.                 if (ob is MemberInfo)
  74.                     return ((MemberInfo)ob).Name;
  75.                 else
  76.                     return ob.ToString();
  77.             }
  78.         }
  79.        
  80.         public virtual void Reset()
  81.         {
  82.             this.index = 0;
  83.             foreach (System.Collections.IEnumerator e in this.enumerators)
  84.                 e.Reset();
  85.             this.visited_names = new SimpleHashtable(16);
  86.         }
  87.     }
  88. }

Developer Fusion