home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / ClsView / HierTree.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  5.2 KB  |  155 lines

  1. /*=====================================================================
  2.   File:      HierTree.cs
  3.  
  4.   Summary:   Brief summary of the file contents and purpose.
  5.  
  6. ---------------------------------------------------------------------
  7.   This file is part of the Microsoft NGWS SDK Code Samples.
  8.  
  9.   Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  10.  
  11. This source code is intended only as a supplement to Microsoft
  12. Development Tools and/or on-line documentation.  See these other
  13. materials for detailed information regarding Microsoft code samples.
  14.  
  15. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. PARTICULAR PURPOSE.
  19. =====================================================================*/
  20.  
  21. namespace LangUtil {
  22.  
  23. using System;
  24. using System.Reflection;
  25. using System.Collections;
  26.  
  27.    public class HTMLHierTree
  28.    {
  29.  
  30.       /**
  31.       * Creates an array consisting of the super- and subclasses of the specified
  32.       * focus type.  
  33.       * 
  34.       * @return An array of Type objects.  The array consists of the super classes, 
  35.       *         followed by the focus Type object, followed by the subclasses. The special
  36.       *         cases occur when the focus is System.Object or an interface, in which
  37.       *         case an empty Type array is returned.
  38.       *
  39.       * @param focus - the Type object for which the hierarchy, or tree, is being
  40.       *                generated.
  41.       * @param allTypes - the set of all types from which to find the super- and
  42.       *                   subclasses of the focus type.
  43.       * @exception ArgumentNullException if focus or allTypes is null. 
  44.       */
  45.  
  46.       public static Type[] GetTree( Type focus )
  47.       {
  48.          if ( focus == null ) 
  49.             throw new ArgumentNullException( "focus" );
  50.  
  51.          if ( focus == Type.GetType( "System.Object" ) ) 
  52.             return new Type[0];
  53.          if ( focus.IsInterface ) 
  54.             return new Type[0];
  55.       
  56.          Type[] ma = GetParents( focus );
  57.          Type[] faces = GetDeclaredInterfaces( focus );
  58.  
  59.          Type[] result = new Type[ma.Length + 1 + faces.Length];
  60.          for ( int i = 0; i < ma.Length; i++ )
  61.          {
  62.             result[i] = ma[i];
  63.          }
  64.          result[ma.Length] = focus;
  65.          for ( int i = 0; i < faces.Length; i++ )
  66.             result[ma.Length + 1 + i] = faces[i];
  67.  
  68.          return result;
  69.       }
  70.        
  71.       public static Type[] GetParents( Type typ )
  72.       {
  73.          if ( typ == null ) 
  74.             throw new ArgumentNullException( "Type called is null." );
  75.  
  76.          ArrayList list = new ArrayList();
  77.  
  78.          // Add the superclasses to the list
  79.          while ( typ.BaseType != null )
  80.          {
  81.             // Add interfaces first, see list.Reverse() below
  82.             Type[] interfaces = GetDeclaredInterfaces( typ.BaseType );
  83.             for ( int i = 0; i < interfaces.Length; i++ )
  84.                list.Add( interfaces[i] );
  85.  
  86.             list.Add( typ.BaseType );
  87.             typ = typ.BaseType;
  88.          }
  89.          list.Reverse();
  90.          return (Type[])list.ToArray( Type.GetType( "System.Type" ) );
  91.       }
  92.  
  93.  
  94.       public static Type[] GetChildren( Type typ, Type[] allTypes )
  95.       {
  96.          if ( typ == null ) 
  97.             throw new ArgumentNullException( "typ" );
  98.          if ( allTypes == null ) 
  99.             throw new ArgumentNullException( "allTypes" );
  100.  
  101.          ArrayList list = new ArrayList();
  102.  
  103.          for ( int i = 0; i < allTypes.Length; i++ )
  104.          {
  105.             // This skips non-public types
  106.             if ( !allTypes[i].IsPublic )
  107.                continue; 
  108.  
  109.             if ( allTypes[i].BaseType == typ )
  110.             {
  111.                list.Add( allTypes[i] );
  112.  
  113.                // Add interfaces
  114.                Type[] interfaces = GetDeclaredInterfaces( allTypes[i] );
  115.                for ( int j = 0; j < interfaces.Length; j++ )
  116.                   list.Add( interfaces[j] );
  117.             }
  118.          }
  119.          return (Type[])list.ToArray( Type.GetType( "System.Type" ) );
  120.       }
  121.  
  122.  
  123.       public static Type[] GetDeclaredInterfaces( Type typ )
  124.       {
  125.          if ( typ == null ) 
  126.             throw new ArgumentNullException( "typ" );
  127.  
  128.          if ( typ.IsInterface ) 
  129.             throw new ArgumentException( "typ cannot be an interface." );
  130.  
  131.          Hashtable hash = new Hashtable();
  132.          for ( Type cur = typ.BaseType; cur != null; cur = cur.BaseType )
  133.          {
  134.             Type[] interfaces = cur.GetInterfaces();
  135.             for ( int i = 0; i < interfaces.Length; i++ )
  136.             {
  137.                if ( !hash.ContainsKey( interfaces[i] ) ) 
  138.                   hash.Add( interfaces[i], null );
  139.             }
  140.           }
  141.       
  142.          Type[] interfaces2 = typ.GetInterfaces();
  143.          ArrayList list = new ArrayList();
  144.          for ( int i = 0; i < interfaces2.Length; i++ )
  145.          {
  146.             if ( !hash.ContainsKey( interfaces2[i] ) ) 
  147.                list.Add( interfaces2[i] );
  148.          }
  149.          Type[] result = (Type[])list.ToArray( typeof( Type ) );
  150.          return result;
  151.       }
  152.    }
  153.  
  154. } // namespace LangUtil
  155.