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

  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. /*
  19.     This class accumulates members from super types and interfaces and then returns them as an array that is sorted
  20.     so that all members with the same name are contiguous and so that members from more derived classes precede members from less derived classes.
  21.     */   
  22.    
  23.     using System;
  24.     using System.Reflection;
  25.     using System.Collections;
  26.    
  27.     public sealed class SuperTypeMembersSorter
  28.     {
  29.         private SimpleHashtable members;
  30.         private ArrayList names;
  31.         private int count;
  32.        
  33.         internal SuperTypeMembersSorter()
  34.         {
  35.             this.members = new SimpleHashtable(64);
  36.             this.names = new ArrayList();
  37.             this.count = 0;
  38.         }
  39.        
  40.         //This routine is to be called in order of derivation.
  41.         internal void Add(MemberInfo[] members)
  42.         {
  43.             foreach (MemberInfo member in members)
  44.                 this.Add(member);
  45.         }
  46.        
  47.         internal void Add(MemberInfo member)
  48.         {
  49.             this.count++;
  50.             string name = member.Name;
  51.             object ob = this.members[name];
  52.             if (ob == null) {
  53.                 this.members[name] = member;
  54.                 this.names.Add(name);
  55.             }
  56.             else if (ob is MemberInfo) {
  57.                 ArrayList mems = new ArrayList(8);
  58.                 mems.Add(ob);
  59.                 mems.Add(member);
  60.                 this.members[name] = mems;
  61.             }
  62.             else
  63.                 ((ArrayList)ob).Add(member);
  64.         }
  65.        
  66.         internal object[] GetMembers()
  67.         {
  68.             object[] result = new object[this.count];
  69.             int i = 0;
  70.             foreach (object name in this.names) {
  71.                 object ob = this.members[name];
  72.                 if (ob is MemberInfo)
  73.                     result[i++] = ob;
  74.                 else {
  75.                     foreach (object m in (ArrayList)ob)
  76.                         result[i++] = m;
  77.                 }
  78.             }
  79.             return result;
  80.         }
  81.     }
  82. }

Developer Fusion