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

  1. /*=====================================================================
  2.   File:      LangType.cs
  3.  
  4.   Summary:   The LangType is the abstract base class that provides 
  5.          language-based translation and basic heirarchy and 
  6.          ordering functionality for the LangUtil namespace. Constructors
  7.          take a Type or a ParameterInfo and either implement or
  8.          declare the members necessary for callers to obtain
  9.          easily language-specific information. Many members must be
  10.          overridden; others are declared in comments, but implemented
  11.          on children to avoid undue casting by callers.
  12.  
  13. ---------------------------------------------------------------------
  14.   This file is part of the Microsoft NGWS SDK Code Samples.
  15.  
  16.   Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  17.  
  18. This source code is intended only as a supplement to Microsoft
  19. Development Tools and/or on-line documentation.  See these other
  20. materials for detailed information regarding Microsoft code samples.
  21.  
  22. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  23. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  24. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  25. PARTICULAR PURPOSE.
  26. =====================================================================*/
  27.  
  28. namespace LangUtil {
  29.  
  30. using System;
  31. using System.IO;
  32. using System.Text;
  33. using System.Reflection;
  34. using System.Collections;
  35.  
  36.    public abstract class LangType 
  37.    {
  38.       internal Type m_TypeObject = null;
  39.       internal string Type = String.Empty;
  40.       internal string ShortType = String.Empty;
  41.       public string ParamVarName = String.Empty;
  42.       internal bool m_IsByRef = false;
  43.       internal ParameterInfo m_ParamInfo = null;
  44.       internal bool m_IsPointer = false;
  45.       internal bool m_IsArray = false;
  46.  
  47.       internal int m_ACCESS_STRING_SIZE = 40;
  48.       internal int m_ATTRIBUTE_STRING_SIZE = 60;
  49.  
  50.       // Constructor takes a Type object and loads the necessary internal variables for
  51.       // children to translate the object easily.
  52.  
  53.       public LangType( Type ThisType )
  54.       {
  55.          try 
  56.          {
  57.             if ( ThisType == null )
  58.                throw new NullReferenceException( "The Type passed to the constructor is null." );
  59.  
  60.             m_TypeObject = ThisType;
  61.  
  62.             if ( m_TypeObject.IsArray && ( m_TypeObject != typeof( Array ) ) )
  63.             {
  64.                Type = GetTypeString( m_TypeObject, true );
  65.                ShortType = GetTypeString( m_TypeObject, false );
  66.             }
  67.             else
  68.             {
  69.                Type = m_TypeObject.FullName;
  70.                ShortType = m_TypeObject.Name;
  71.             }
  72.          }
  73.          catch ( Exception Ex )
  74.          {
  75.             throw new Exception( "Error in LangType constructor." , Ex );
  76.          }
  77.       }
  78.  
  79.       // Constructor takes a ParameterInfo. Extra work here includes loading the boolean flags that
  80.       // indicate whether the ParameterInfo IsByRef, IsPointer, or IsArray
  81.       public LangType( ParameterInfo ParamInfo )
  82.       {
  83.          try
  84.          {
  85.             m_ParamInfo = ParamInfo;
  86.             m_TypeObject = ParamInfo.ParameterType;
  87.             ParamVarName = ParamInfo.Name;
  88.  
  89.             Type = GetTypeString( m_TypeObject, true );
  90.             ShortType = GetTypeString( m_TypeObject, false );
  91.  
  92.             // ByReference information for parameters
  93.             m_IsByRef = ParamInfo.ParameterType.IsByRef;
  94.             m_IsPointer = ParamInfo.ParameterType.IsPointer;
  95.             m_IsArray = ParamInfo.ParameterType.IsArray;
  96.          }
  97.          catch ( Exception Ex )
  98.          {
  99.             throw new Exception( "Error in LangType constructor at: " /*+ Ex.GetMethod().Name*/, Ex );
  100.          }
  101.       }
  102.  
  103.       /*
  104.       //
  105.       //  This always returns 
  106.       //  the appropriate <namespace>.<className> form for all types, regardless of 
  107.       //  their underlying elements, with one exception: It doesn't properly handle
  108.       //  multidimensional arrays, which it returns instead as "System.Array".
  109.       //
  110.       //  This method is called by the child constructor to set the name for parameter types
  111.       //  and return types properly; usually, the child will call this and then add the 
  112.       //  appropriate *, &, or other markers appropriate to the language, and return that
  113.       //  language-specific value through the FullClassName or ClassName properties.
  114.       //
  115.       */
  116.  
  117.       internal string GetTypeString( Type ThisType, bool WantFullName )
  118.       {
  119.          StringBuilder TypeString = new StringBuilder( String.Empty );
  120.       
  121.          if ( ThisType.HasElementType )
  122.          {
  123.             if ( ThisType.IsArray && !( ThisType.Name.Equals( "Array" ) ) )
  124.             {
  125.                m_IsArray = true;
  126.                if ( WantFullName )
  127.                   TypeString.Append( ThisType.GetElementType().FullName );
  128.                else
  129.                   TypeString.Append( ThisType.GetElementType().Name );
  130.             }
  131.             else if ( ThisType.Name.Equals( "Array" ) )
  132.             {
  133.                if ( WantFullName )
  134.                   TypeString.Append( "System.Array" );
  135.                else
  136.                   TypeString.Append( "Array" );
  137.             
  138.             }
  139.             else if ( ThisType.IsByRef )
  140.             {
  141.                if ( WantFullName )
  142.                   TypeString.Append( ThisType.GetElementType().FullName );
  143.                else
  144.                   TypeString.Append( ThisType.GetElementType().Name );
  145.             }
  146.             else
  147.             {
  148.                if ( WantFullName )
  149.                   TypeString.Append( ThisType.GetElementType().FullName );
  150.                else
  151.                   TypeString.Append( ThisType.GetElementType().Name );
  152.             }
  153.          }
  154.          else
  155.          {
  156.             if ( WantFullName )
  157.                TypeString.Append( ThisType.FullName );
  158.             else
  159.                TypeString.Append( ThisType.Name );
  160.          }
  161.          return TypeString.ToString();
  162.       }
  163.  
  164.       // Default implementation does nothing
  165.       virtual public string TranslateTypeName( string name )
  166.       {
  167.          return name;
  168.       }
  169.  
  170.       // Returns the fully-qualified classname.
  171.       public String FullClassName
  172.       {
  173.          virtual get { return Type; }
  174.       }
  175.  
  176.       public String ClassName 
  177.       {
  178.          get { return TranslateTypeName( ShortType ); }
  179.       }
  180.  
  181.       public bool IsByRef
  182.       {
  183.          virtual get { return m_IsByRef; }
  184.       }
  185.  
  186.       public bool IsPointer
  187.       {
  188.          virtual get { return m_IsPointer; }
  189.       }
  190.  
  191.       public bool IsArray
  192.       {
  193.          virtual get { return m_IsArray; }
  194.       }
  195.  
  196.       public bool IsRealArray
  197.       {
  198.          virtual get { return ( m_IsArray && ThisType != typeof( Array ) ) ? true : false; }
  199.       }
  200.  
  201.       public Type ThisType 
  202.       { 
  203.          virtual get{ return m_TypeObject; }
  204.       }
  205.  
  206.       /*
  207.       // TextDeclaration must be overridden by the child classes to return a string that
  208.       // is a legal declaration in the language of the child class to which the caller has
  209.       //  a reference.
  210.       */
  211.       public string TextDeclaration
  212.       {
  213.          abstract get;
  214.       }
  215.  
  216.       /*
  217.       // HTMLDeclaration must be overridden by the child classes to return a string that
  218.       // is a legal declaration in the language of the child class to which the caller has
  219.       //  a reference.
  220.       */
  221.       public string HTMLDeclaration
  222.       {
  223.          abstract get;
  224.       }
  225.  
  226.       /*
  227.       // XMLDeclaration must be overridden by the child classes to return a string that
  228.       // is a legal declaration in the language of the child class to which the caller has
  229.       //  a reference.
  230.  
  231.         Right now, however, there is no implementation. 
  232.       */
  233.       public string XMLDeclaration
  234.       {
  235.          abstract get;
  236.       }
  237.  
  238.       /*
  239.       // OrderedConstructors returns an array of ConstructorInfos that are all the 
  240.       //  constructorinfos returned by the Type passed to this class's constructor, 
  241.       // sorted by name. Right now, this sort only moves .cctor to the front of the
  242.       // list followed by .ctor. 
  243.       // Property returns an array of all constructors, including .cctor. Caller is
  244.       // responsible for stripping out any constructors that are unwanted.
  245.       */
  246.  
  247.       public ConstructorInfo[] OrderedConstructors
  248.       {
  249.          virtual get
  250.          {
  251.             ConstructorInfo[] Constructors = null;
  252.  
  253.             try
  254.             {
  255.                Constructors = m_TypeObject.GetConstructors( BindingFlags.LookupAll );
  256.                ConstructorComparer ICompConst = new ConstructorComparer();
  257.                Array.Sort( Constructors, ICompConst );
  258.             }
  259.             catch ( Exception Ex )
  260.             {
  261.                throw new Exception( "Occurred in LangType.OrderedConstructors.", Ex );
  262.             }
  263.             return Constructors;
  264.          }
  265.       }
  266.  
  267.       /*
  268.       // OrderedMethods returns an array of MethodInfos that are all the 
  269.       //  MethodInfos returned by the Type passed to this class's constructor, sorted
  270.       // by method name. Does not alter the order of identically named methods.
  271.       // Property returns an array of all methods, including private methods. Caller is
  272.       // responsible for stripping out any methods that are unwanted.
  273.       */
  274.       public MethodInfo[] OrderedMethods
  275.       {
  276.          virtual get
  277.          {
  278.             MethodInfo[] Methods = null;
  279.  
  280.             try
  281.             {
  282.                Methods = m_TypeObject.GetMethods( BindingFlags.LookupAll );
  283.  
  284.                // Order the member categories
  285.                MethodComparer ICompMet = new MethodComparer();
  286.                Array.Sort( Methods, ICompMet );
  287.             }
  288.             catch ( Exception Ex )
  289.             {
  290.                throw new Exception( "Occurred in LangType.OrderedMethods.", Ex );
  291.             }
  292.             return Methods;
  293.          }
  294.       }
  295.  
  296.       /*
  297.       // OrderedProperties returns an array of PropertyInfos that are all the 
  298.       //  PropertyInfos returned by the Type passed to this class's constructor, sorted
  299.       // by property name. Does not alter the order of identically named properties.
  300.       // Property returns an array of all properties, including nonpublic properties. Caller is
  301.       // responsible for stripping out any properties that are unwanted.
  302.       */
  303.       public PropertyInfo[] OrderedProperties
  304.       {
  305.          virtual get
  306.          {
  307.             PropertyInfo[] Properties = null;
  308.             try
  309.             {
  310.                Properties = m_TypeObject.GetProperties( BindingFlags.LookupAll );
  311.                PropertyComparer ICompProp = new PropertyComparer();
  312.                Array.Sort( Properties, ICompProp );
  313.             }
  314.             catch ( Exception Ex )
  315.             {
  316.                throw new Exception( "Occurred in LangType.OrderedProperties", Ex );
  317.             }
  318.             return Properties;
  319.          }
  320.       }
  321.  
  322.       /*
  323.       // OrderedFields returns an array of FieldInfos that are all the 
  324.       //  FieldInfos returned by the Type passed to this class's constructor, sorted
  325.       // by field name. Does not alter the order of identically named fields.
  326.       // Property returns an array of all fields, including nonpublic fields. Caller is
  327.       // responsible for stripping out any fields that are unwanted.
  328.       */
  329.       public FieldInfo[] OrderedFields
  330.       {
  331.          virtual get
  332.          {
  333.             FieldInfo[] Fields = null;
  334.             try
  335.             {
  336.                Fields = m_TypeObject.GetFields( BindingFlags.LookupAll );
  337.                FieldComparer ICompField = new FieldComparer();
  338.                Array.Sort( Fields, ICompField );
  339.             }
  340.             catch ( Exception Ex )
  341.             {
  342.                throw new Exception( "Occurred in LangType.OrderedFields.", Ex );
  343.             }
  344.             return Fields;
  345.          }
  346.       }
  347.  
  348.       /*
  349.       // OrderedEvents returns an array of EventInfos that are all the 
  350.       //  EventInfos returned by the Type passed to this class's constructor, sorted
  351.       // by event name. Does not alter the order of identically named events.
  352.       // Property returns an array of all events, including nonpublic events. Caller is
  353.       // responsible for stripping out any events that are unwanted.
  354.       */
  355.       public EventInfo[] OrderedEvents
  356.       {
  357.          virtual get
  358.          {
  359.             EventInfo[] Events = null;
  360.             try
  361.             {
  362.                Events = m_TypeObject.GetEvents( BindingFlags.LookupAll );
  363.                EventComparer ICompEvent = new EventComparer();
  364.                Array.Sort( Events, ICompEvent );
  365.             }
  366.             catch ( Exception Ex )
  367.             {
  368.                throw new Exception( "Occurred in LangType.OrderedEvents.", Ex );
  369.             }
  370.             return Events;
  371.          }
  372.       }
  373.    }
  374.  
  375. } // namespace LangUtil
  376.