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

  1. /*=====================================================================
  2.   File:      MCConstructorInfo.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.  
  22. namespace LangUtil {
  23.  
  24. using System;
  25. using System.Text;
  26. using System.Reflection;
  27. using System.Collections;
  28.  
  29.    public class MCConstructorInfo : LangMember
  30.    {
  31.       internal String m_Name = String.Empty;
  32.       internal ArrayList m_Parameters = new ArrayList();
  33.       internal ConstructorInfo m_ThisConstructorInfo = null;
  34.       internal Type m_ThisType = null;
  35.       internal bool m_Overloads = false;
  36.  
  37.       public MCConstructorInfo( ConstructorInfo myConstructor )
  38.       {
  39.          if ( myConstructor == null )
  40.             throw new NullReferenceException( "The ConstructorInfo passed to the constructor is null." );
  41.  
  42.          m_ThisConstructorInfo = myConstructor;
  43.          m_Name = myConstructor.Name;
  44.          m_ThisType  = myConstructor.ReflectedType;
  45.  
  46.          // Initialize "m_Overloads" to true if there is more than 1 non-private constructor         
  47.          ConstructorInfo[] allConstructors = myConstructor.DeclaringType.GetConstructors( BindingFlags.NonPublic );
  48.          int cnt = 0;
  49.          for ( int i = 0; i < allConstructors.Length; i++ )
  50.          {
  51.             if ( !allConstructors[i].IsPrivate ) 
  52.                cnt++;
  53.          }
  54.          if ( cnt > 1 ) 
  55.             m_Overloads = true;
  56.       
  57.          // Determine whether method can accept variable-length arguments
  58.          if ( myConstructor.CallingConvention == CallingConventions.VarArgs )
  59.             m_VarArgs = true;
  60.  
  61.          // Obtain list of indexed properties 
  62.          ParameterInfo[] myParams = myConstructor.GetParameters();
  63.  
  64.          for ( int x = 0; x < myParams.Length; x++ )
  65.          {
  66.             m_Parameters.Add( new MCLangType( myParams[x] ) );
  67.          }   
  68.       }
  69.  
  70.       public Type ThisType
  71.       {
  72.          override get { return m_ThisType; }
  73.       }
  74.    
  75.        public String Attributes
  76.        {
  77.          override get 
  78.          {
  79.             StringBuilder sb = new StringBuilder();
  80.             
  81.             // Append access modifier
  82.             if ( m_ThisConstructorInfo.IsPublic )
  83.                sb.Append( "public: " );
  84.             else if ( m_ThisConstructorInfo.IsPrivate )
  85.                sb.Append( "private: " );
  86.             else if ( m_ThisConstructorInfo.IsFamily )
  87.                sb.Append( "protected: " );
  88.             // What  the correct keywords for these???:
  89.             else if ( m_ThisConstructorInfo.IsAssembly )
  90.                sb.Append( "Assembly: " );
  91.             else if ( m_ThisConstructorInfo.IsFamilyAndAssembly )
  92.                sb.Append( "FamilyAndAssembly: " );
  93.             else if ( m_ThisConstructorInfo.IsFamilyOrAssembly )
  94.                sb.Append( "FamilyOrAssembly: " );
  95.             
  96.             return sb.ToString();
  97.          }
  98.        }
  99.  
  100.       public String ShortTextDeclaration
  101.       {
  102.          get
  103.          {
  104.             StringBuilder Syntax = new StringBuilder( m_ThisConstructorInfo.ReflectedType.Name + "(" );
  105.             for ( int i = 0; i < m_Parameters.Count; i++ ) 
  106.             { 
  107.                Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
  108.                if ( ( (MCLangType)m_Parameters[i] ).m_IsArray )
  109.                   Syntax.Append( "[]" );
  110.                if ( ( (MCLangType)m_Parameters[i] ).IsByRef )
  111.                   Syntax.Append( "*" );
  112.  
  113.                // Writes the comma, or the last break
  114.                if ( i != ( m_Parameters.Count - 1 ) )
  115.                   Syntax.Append( ", " );
  116.             } 
  117.             Syntax.Append( ")" );
  118.             return Syntax.ToString();
  119.          }
  120.       }
  121.  
  122.       public String TextDeclaration
  123.       {
  124.          override get
  125.          {
  126.             StringBuilder Syntax = new StringBuilder( this.Attributes + m_ThisConstructorInfo.ReflectedType.Name + "(" );
  127.             for ( int i = 0; i < m_Parameters.Count; i++ ) 
  128.             { 
  129.                Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
  130.                if ( ( (MCLangType)m_Parameters[i] ).m_IsArray )
  131.                   Syntax.Append( "[]" );
  132.                if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
  133.                   Syntax.Append( "*" );
  134.                if ( ( (MCLangType)m_Parameters[i] ).m_IsPointer )
  135.                   Syntax.Append( "*" );
  136.                Syntax.Append( " " + ((MCLangType)m_Parameters[i]).ParamVarName );
  137.  
  138.                // Writes the comma, or the last break
  139.                if ( i != ( m_Parameters.Count - 1 ) )
  140.                   Syntax.Append( ", " );
  141.             } 
  142.             Syntax.Append( ")" );
  143.             Syntax.Append( ";" );
  144.             return Syntax.ToString();
  145.          }
  146.       }
  147.  
  148.       public String HTMLDeclaration
  149.       {
  150.          override get
  151.          {
  152.             StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
  153.             Syntax.Append( "<span style=\"color:red\">[MC++]</span><BR>" );
  154.             
  155.             Syntax.Append( "<B>" );
  156.             Syntax.Append( this.Attributes + m_ThisConstructorInfo.ReflectedType.Name + "(" );
  157.             for ( int i = 0; i < m_Parameters.Count; i++ ) 
  158.             {                
  159.                Syntax.Append( "<BR>    " );
  160.                Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
  161.                if ( ( (MCLangType)m_Parameters[i] ).m_IsArray )
  162.                   Syntax.Append( "[]" );
  163.                if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
  164.                   Syntax.Append( "*" );
  165.                Syntax.Append( " <I>" + ( (MCLangType)m_Parameters[i] ).ParamVarName + "</I>" );
  166.  
  167.                // Writes the comma, or the last break
  168.                if ( i != (m_Parameters.Count - 1 ) )
  169.                   Syntax.Append( ", " );
  170.                else 
  171.                   Syntax.Append( "<BR>" );
  172.             }
  173.             Syntax.Append( ")" );
  174.             Syntax.Append( ";" );
  175.             Syntax.Append( "</B></PRE>" );
  176.          
  177.             return Syntax.ToString();
  178.          }
  179.       }
  180.  
  181.       public String LangName
  182.       {
  183.          get { return m_ThisConstructorInfo.ReflectedType.Name; }
  184.       }
  185.  
  186.       public String Name 
  187.       {         
  188.          override get{ return m_Name; }
  189.       }
  190.       
  191.       public ConstructorInfo ThisConstructorInfo
  192.       {
  193.          get { return m_ThisConstructorInfo; }
  194.       }
  195.  
  196.       public bool Overloads
  197.       {
  198.          get { return m_Overloads; }
  199.       }
  200.  
  201.       public override bool IsConstructor()
  202.       {
  203.          return true;
  204.       }
  205.    }
  206.  
  207. } // namespace LangUtil
  208.