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

  1. /*=====================================================================
  2.   File:      VisualBasicMethodInfo.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.Text;
  25. using System.Reflection;
  26. using System.Collections;
  27.  
  28.    public class VisualBasicMethodInfo : LangMember
  29.    {
  30.       internal string m_Name = String.Empty;
  31.       internal MethodInfo m_ThisMethodInfo = null;
  32.       internal VisualBasicLangType m_ReturnLangType = null;
  33.       internal ArrayList m_Parameters = null;
  34.  
  35.       public VisualBasicMethodInfo( MethodInfo myMethod )
  36.       {
  37.          if ( myMethod == null )
  38.             throw new NullReferenceException( "The MethodInfo passed to the constructor is null." );
  39.  
  40.          // Determine method name and return type
  41.          m_Name = myMethod.Name;
  42.          m_ThisMethodInfo = myMethod;
  43.          m_ReturnLangType = new VisualBasicLangType( myMethod.ReturnType );
  44.  
  45.          // Determine whether method can accept variable-length arguments
  46.          if ( myMethod.CallingConvention == CallingConventions.VarArgs )
  47.             m_VarArgs = true;
  48.  
  49.          // Obtain parameter details
  50.          ParameterInfo[] myParams = myMethod.GetParameters();
  51.          
  52.          // Set arraylist size
  53.          m_Parameters = new ArrayList( myParams.Length );
  54.  
  55.          for ( int x = 0; x < myParams.Length; x++ )
  56.          {
  57.             m_Parameters.Add( new VisualBasicLangType( myParams[x] ) );
  58.          }   
  59.       }
  60.  
  61.       public MethodInfo ThisMethodInfo
  62.       {
  63.          get { return m_ThisMethodInfo; }
  64.       }
  65.  
  66.       public bool IsOverloaded
  67.       {
  68.          get
  69.          {
  70.             MethodInfo[] allMethods = m_ThisMethodInfo.DeclaringType.GetMethods( BindingFlags.NonPublic );
  71.             int cnt = 0;
  72.             for ( int i = 0; i < allMethods.Length; i++ )
  73.             {
  74.                if ( !allMethods[i].IsPrivate && allMethods[i].Name.Equals( m_Name ) ) 
  75.                   cnt++;
  76.             }
  77.             if ( cnt > 1 ) 
  78.                return true;
  79.             else 
  80.                return false;
  81.          }
  82.       }
  83.  
  84.       public VisualBasicLangType ReturnLangType
  85.       {
  86.          get { return m_ReturnLangType; }
  87.       }
  88.  
  89.       public ArrayList Parameters
  90.       {
  91.          get { return m_Parameters; }
  92.       }
  93.  
  94.       public Type ThisType
  95.       {
  96.          override get { return m_ThisMethodInfo.ReflectedType; }
  97.       }
  98.  
  99.       public string Name
  100.       {
  101.          override get { return m_Name; }
  102.       }
  103.  
  104.       public string Attributes
  105.       {
  106.          override get 
  107.          { 
  108.             StringBuilder sb = new StringBuilder();
  109.             if ( m_ThisMethodInfo.IsAbstract )
  110.                sb.Append( "MustOverride " );
  111.             
  112.             if ( m_ThisMethodInfo.IsFinal && !m_ThisMethodInfo.DeclaringType.IsSealed && 
  113.                  !m_ThisMethodInfo.DeclaringType.IsInterface )
  114.                sb.Append( "CantOverride " );
  115.  
  116.             try 
  117.             {
  118.                m_ThisMethodInfo.ReflectedType.GetMethod( m_ThisMethodInfo.Name );
  119.             }
  120.             catch
  121.             {
  122.                sb.Append( "Overloads " );
  123.             }
  124.          
  125.             if ( m_ThisMethodInfo.IsPublic )
  126.                sb.Append( "Public " );
  127.             else if ( m_ThisMethodInfo.IsPrivate )
  128.                sb.Append( "Private " );
  129.             else if ( m_ThisMethodInfo.IsFamily )
  130.                sb.Append( "Protected " );
  131.             else if ( m_ThisMethodInfo.IsAssembly )
  132.                sb.Append( "Assembly " );
  133.             else if ( m_ThisMethodInfo.IsFamilyAndAssembly )
  134.                sb.Append( "FamilyAndAssembly " );
  135.             else if ( m_ThisMethodInfo.IsFamilyOrAssembly )
  136.                sb.Append( "FamilyOrAssembly " );
  137.  
  138.             if ( m_ThisMethodInfo.IsStatic )
  139.                sb.Append( "Shared " );
  140.             
  141.             if ( m_ThisMethodInfo.ReturnType == typeof( Void ) )
  142.                sb.Append( "Sub " );
  143.             else
  144.                sb.Append( "Function " );
  145.             
  146.             return sb.ToString();         
  147.          }
  148.       }
  149.  
  150.       public string ShortTextDeclaration
  151.       {
  152.          get
  153.          {
  154.             StringBuilder Syntax = new StringBuilder( m_Name + "(" );
  155.             for ( int i = 0; i < m_Parameters.Count; i++ ) 
  156.             { 
  157.                Syntax.Append( ( (VisualBasicLangType)m_Parameters[i] ).ClassName );
  158.  
  159.                if ( ( (VisualBasicLangType)m_Parameters[i] ).IsRealArray )
  160.                   Syntax.Append( "()" );
  161.  
  162.                // Writes the comma, or the last break
  163.                if ( i != ( m_Parameters.Count - 1 ) )
  164.                   Syntax.Append( ", " );
  165.                else
  166.                {
  167.                }
  168.             }
  169.             Syntax.Append( ")" );
  170.             return Syntax.ToString();
  171.          }
  172.       }
  173.  
  174.       public string TextDeclaration
  175.       {
  176.          override get
  177.          {
  178.             StringBuilder Syntax = new StringBuilder( Attributes );
  179.             Syntax.Append( m_Name );
  180.             Syntax.Append( "(" );
  181.  
  182.             for ( int i = 0; i < m_Parameters.Count; i++ )
  183.             { 
  184.                if ( ( (VisualBasicLangType)m_Parameters[i] ).m_IsByRef )
  185.                   Syntax.Append( "ByRef " );
  186.                else 
  187.                   Syntax.Append( "ByVal " );
  188.                
  189.                string strClassName = ( (VisualBasicLangType)m_Parameters[i] ).ClassName;
  190.                // Strip the brackets here, if any
  191.                bool bBrackets = false;
  192.                if ( strClassName.Substring( strClassName.Length - 2 ) == "[]" )
  193.                {
  194.                   bBrackets = true;
  195.                   strClassName = strClassName.Substring( 0, strClassName.Length - 2 );
  196.                }
  197.  
  198.                Syntax.Append( ( (VisualBasicLangType)m_Parameters[i] ).ParamVarName );
  199.                
  200.                if ( bBrackets || ( (VisualBasicLangType)m_Parameters[i] ).IsRealArray )
  201.                   Syntax.Append( "()" );
  202.  
  203.                Syntax.Append( " As " + strClassName );
  204.  
  205.                // Writes the comma, or the last break
  206.                if ( i != ( m_Parameters.Count - 1 ) )
  207.                   Syntax.Append( ", " );
  208.                else
  209.                {
  210.                   // If there's varargs do the right thing, then finish the syntax block
  211.                   if ( VarArgs )
  212.                      Syntax.Append( ", ..." );
  213.                }
  214.             } 
  215.  
  216.             Syntax.Append( ")" );
  217.  
  218.             if ( !m_ReturnLangType.ClassName.Equals( "Void" ) )
  219.             {
  220.                Syntax.Append( " As " );
  221.                if ( m_ReturnLangType.m_IsByRef )
  222.                   Syntax.Append( "ByRef " );
  223.                Syntax.Append( m_ReturnLangType.ClassName );
  224.                if ( m_ReturnLangType.IsRealArray )
  225.                   Syntax.Append( "()" );
  226.             }
  227.             return Syntax.ToString();
  228.          }
  229.       }
  230.  
  231.       public string HTMLDeclaration
  232.       {
  233.          override get
  234.          {
  235.             StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
  236.             Syntax.Append( "<span style=\"color:red\">[Visual Basic]</span><BR><B>" );
  237.  
  238.             Syntax.Append( Attributes );
  239.             Syntax.Append( m_Name );
  240.             Syntax.Append( "(" );
  241.             for ( int i = 0; i < m_Parameters.Count; i++ ) 
  242.             { 
  243.                Syntax.Append( " _" );
  244.                Syntax.Append( "<BR>    " );
  245.                if ( ( (VisualBasicLangType)m_Parameters[i]).IsByRef )
  246.                   Syntax.Append( "ByRef " );
  247.                else 
  248.                   Syntax.Append( "ByVal " );
  249.                Syntax.Append( "<I>" + ( (VisualBasicLangType)m_Parameters[i] ).ParamVarName + "</I>" );
  250.  
  251.                if ( ( (VisualBasicLangType)m_Parameters[i] ).IsRealArray )
  252.                   Syntax.Append( "()" );
  253.                
  254.                Syntax.Append( " As " + ( (VisualBasicLangType)m_Parameters[i] ).ClassName );
  255.                // Writes the comma, or the last break
  256.                if ( i != ( m_Parameters.Count - 1 ) )
  257.                   Syntax.Append( ", " );
  258.                else
  259.                {
  260.                   if ( VarArgs )
  261.                   {
  262.                      Syntax.Append( ", -<BR/>    ..." );
  263.                   }
  264.                   Syntax.Append( " _<BR/>" );
  265.                }
  266.             } 
  267.             Syntax.Append( ")" );
  268.             if ( !m_ReturnLangType.ClassName.Equals( "Void" ) ){
  269.                    Syntax.Append( " As " );
  270.                 if ( m_ReturnLangType.m_IsByRef )
  271.                    Syntax.Append( "ByRef " );
  272.                 Syntax.Append( m_ReturnLangType.ClassName );
  273.                 if ( m_ReturnLangType.IsRealArray )
  274.                    Syntax.Append( "()" );
  275.             }
  276.             Syntax.Append( "</B></PRE>" );
  277.             return Syntax.ToString();
  278.          }
  279.       }
  280.  
  281.       public override bool IsMethod()
  282.       {
  283.          return true;
  284.       }       
  285.    }
  286.  
  287. } // namespace LangUtil
  288.