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

  1. /*=====================================================================
  2.   File:      Util.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.  
  26.    public class Util
  27.    {   
  28.       /*************************************************
  29.             
  30.          GetHKey  the method that generates the 
  31.          id element that I use as the hash key.
  32.  
  33.       **************************************************/
  34.  
  35.       public static String GetHKey( MemberInfo mmm )
  36.       {
  37.          String ID = String.Empty;
  38.          // Methods and Constructors
  39.          if ( mmm is MethodBase )
  40.          {
  41.             // So that the hash is created properly between constructors,
  42.             // whose name is now .ctor.
  43.             if ( mmm is ConstructorInfo )
  44.                ID = mmm.DeclaringType.Name + " (";
  45.             else
  46.                ID = mmm.Name + " (";
  47.             ParameterInfo[] ps = ((MethodBase)mmm ).GetParameters();
  48.             for ( int j = 0; j < ps.Length; j++ )
  49.             {
  50.                ID = ID + ps[j].ParameterType.Name;
  51.                if ( j < ps.Length - 1 ) 
  52.                   ID = ID + ", ";
  53.             }
  54.             ID = ID + ")";
  55.             // Append return type for methods
  56.             if ( mmm is MethodInfo )
  57.                ID = ID + " " + ( (MethodInfo )mmm ).ReturnType.Name;
  58.          }
  59.          
  60.          // Properties
  61.          else if ( mmm is PropertyInfo ) 
  62.          {
  63.             ID = mmm.Name;
  64.             ParameterInfo[] ps = ( (PropertyInfo)mmm ).GetIndexParameters();
  65.             if ( ps.Length > 0 ) 
  66.             {
  67.                ID = ID + " [";
  68.                for ( int j = 0; j < ps.Length; j++ )
  69.                {
  70.                   ID = ID + ps[j].ParameterType.Name;
  71.                   if ( j < ps.Length - 1) 
  72.                      ID = ID + ", ";
  73.                }
  74.                ID = ID + "]";
  75.             }
  76.             // append "return" type.  Note: GetType() is NYI on PropertyInfo
  77.             // NEED TO FIX THIS, but perhaps we don't have any write-only properties
  78.             if ( ( (PropertyInfo)mmm).CanRead )
  79.                ID = ID + " " + ( (PropertyInfo)mmm).GetGetMethod( true ).ReturnType.Name;
  80.             else
  81.                ID = ID + " " + "WriteOnly"; // I don't know the correct semantics/syntax for properties            
  82.          }
  83.          // Fields
  84.          else if ( mmm is FieldInfo )
  85.             ID = mmm.Name + " " + ( (FieldInfo)mmm ).FieldType.Name;
  86.  
  87.          // Events
  88.          else if ( mmm is EventInfo )
  89.          {
  90.             // ID = "Event " + mmm.Name + " " + ( (EventInfo)mmm ).EventHandler.Name;
  91.          }
  92.  
  93.          else
  94.          {
  95.             Console.WriteLine( "UNABLE TO CREATE HASH ID FOR: " + mmm.Name );
  96.             return "ERROR";
  97.          }         
  98.          return ID;
  99.       }   
  100.    }
  101.  
  102. } // namespace LangUtil
  103.