home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: HierTree.cs
-
- Summary: Brief summary of the file contents and purpose.
-
- ---------------------------------------------------------------------
- This file is part of the Microsoft NGWS SDK Code Samples.
-
- Copyright (C) 2000 Microsoft Corporation. All rights reserved.
-
- This source code is intended only as a supplement to Microsoft
- Development Tools and/or on-line documentation. See these other
- materials for detailed information regarding Microsoft code samples.
-
- THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
- KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
- PARTICULAR PURPOSE.
- =====================================================================*/
-
- namespace LangUtil {
-
- using System;
- using System.Reflection;
- using System.Collections;
-
- public class HTMLHierTree
- {
-
- /**
- * Creates an array consisting of the super- and subclasses of the specified
- * focus type.
- *
- * @return An array of Type objects. The array consists of the super classes,
- * followed by the focus Type object, followed by the subclasses. The special
- * cases occur when the focus is System.Object or an interface, in which
- * case an empty Type array is returned.
- *
- * @param focus - the Type object for which the hierarchy, or tree, is being
- * generated.
- * @param allTypes - the set of all types from which to find the super- and
- * subclasses of the focus type.
- * @exception ArgumentNullException if focus or allTypes is null.
- */
-
- public static Type[] GetTree( Type focus )
- {
- if ( focus == null )
- throw new ArgumentNullException( "focus" );
-
- if ( focus == Type.GetType( "System.Object" ) )
- return new Type[0];
- if ( focus.IsInterface )
- return new Type[0];
-
- Type[] ma = GetParents( focus );
- Type[] faces = GetDeclaredInterfaces( focus );
-
- Type[] result = new Type[ma.Length + 1 + faces.Length];
- for ( int i = 0; i < ma.Length; i++ )
- {
- result[i] = ma[i];
- }
- result[ma.Length] = focus;
- for ( int i = 0; i < faces.Length; i++ )
- result[ma.Length + 1 + i] = faces[i];
-
- return result;
- }
-
- public static Type[] GetParents( Type typ )
- {
- if ( typ == null )
- throw new ArgumentNullException( "Type called is null." );
-
- ArrayList list = new ArrayList();
-
- // Add the superclasses to the list
- while ( typ.BaseType != null )
- {
- // Add interfaces first, see list.Reverse() below
- Type[] interfaces = GetDeclaredInterfaces( typ.BaseType );
- for ( int i = 0; i < interfaces.Length; i++ )
- list.Add( interfaces[i] );
-
- list.Add( typ.BaseType );
- typ = typ.BaseType;
- }
- list.Reverse();
- return (Type[])list.ToArray( Type.GetType( "System.Type" ) );
- }
-
-
- public static Type[] GetChildren( Type typ, Type[] allTypes )
- {
- if ( typ == null )
- throw new ArgumentNullException( "typ" );
- if ( allTypes == null )
- throw new ArgumentNullException( "allTypes" );
-
- ArrayList list = new ArrayList();
-
- for ( int i = 0; i < allTypes.Length; i++ )
- {
- // This skips non-public types
- if ( !allTypes[i].IsPublic )
- continue;
-
- if ( allTypes[i].BaseType == typ )
- {
- list.Add( allTypes[i] );
-
- // Add interfaces
- Type[] interfaces = GetDeclaredInterfaces( allTypes[i] );
- for ( int j = 0; j < interfaces.Length; j++ )
- list.Add( interfaces[j] );
- }
- }
- return (Type[])list.ToArray( Type.GetType( "System.Type" ) );
- }
-
-
- public static Type[] GetDeclaredInterfaces( Type typ )
- {
- if ( typ == null )
- throw new ArgumentNullException( "typ" );
-
- if ( typ.IsInterface )
- throw new ArgumentException( "typ cannot be an interface." );
-
- Hashtable hash = new Hashtable();
- for ( Type cur = typ.BaseType; cur != null; cur = cur.BaseType )
- {
- Type[] interfaces = cur.GetInterfaces();
- for ( int i = 0; i < interfaces.Length; i++ )
- {
- if ( !hash.ContainsKey( interfaces[i] ) )
- hash.Add( interfaces[i], null );
- }
- }
-
- Type[] interfaces2 = typ.GetInterfaces();
- ArrayList list = new ArrayList();
- for ( int i = 0; i < interfaces2.Length; i++ )
- {
- if ( !hash.ContainsKey( interfaces2[i] ) )
- list.Add( interfaces2[i] );
- }
- Type[] result = (Type[])list.ToArray( typeof( Type ) );
- return result;
- }
- }
-
- } // namespace LangUtil
-