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

  1. /*=====================================================================
  2.   File:      MCMethodInfo.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 MCMethodInfo : LangMember
  29.    {
  30.       internal String m_Name = String.Empty;
  31.       internal MethodInfo m_ThisMethodInfo = null;
  32.       internal MCLangType m_ReturnLangType = null;
  33.       internal ArrayList m_Parameters = null;
  34.  
  35.       public MCMethodInfo( 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 MCLangType( myMethod.ReturnType );
  44.  
  45.          // Obtain parameter details
  46.          ParameterInfo[] myParams = myMethod.GetParameters();
  47.  
  48.          // Set arraylist size
  49.          m_Parameters = new ArrayList(myParams.Length);
  50.  
  51.          // Determine whether method can accept variable-length arguments
  52.          if ( myMethod.CallingConvention == CallingConventions.VarArgs )
  53.             m_VarArgs = true;
  54.          
  55.          for ( int x = 0; x < myParams.Length; x++ )
  56.          {
  57.             m_Parameters.Add( new MCLangType( myParams[x] ) );
  58.          }
  59.       }
  60.  
  61.       public String Name
  62.       {
  63.          override get { return m_Name; }
  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 MethodInfo ThisMethodInfo
  85.       {
  86.          get { return m_ThisMethodInfo; }
  87.       }
  88.  
  89.       public MCLangType ReturnLangType
  90.       {
  91.          get { return m_ReturnLangType; }
  92.       }
  93.  
  94.       public ArrayList Parameters
  95.       {
  96.          get { return m_Parameters; }
  97.       }
  98.  
  99.       public Type ThisType
  100.       {
  101.          override get { return m_ThisMethodInfo.ReflectedType; }
  102.  
  103.       }
  104.  
  105.       public string Attributes
  106.       {
  107.          override get 
  108.          {            
  109.             StringBuilder sb = new StringBuilder();
  110.             
  111.             if ( m_ThisMethodInfo.IsPublic )
  112.                sb.Append( "public: " );
  113.             else if ( m_ThisMethodInfo.IsPrivate )
  114.                sb.Append( "private: " );
  115.             else if ( m_ThisMethodInfo.IsFamily )
  116.                sb.Append( "protected: " );
  117.             else if ( m_ThisMethodInfo.IsAssembly )
  118.                sb.Append( "Assembly: " );
  119.             else if ( m_ThisMethodInfo.IsFamilyAndAssembly )
  120.                sb.Append( "FamilyAndAssembly: " );
  121.             else if ( m_ThisMethodInfo.IsFamilyOrAssembly )
  122.                sb.Append( "FamilyOrAssembly: " );
  123.             
  124.             if ( m_ThisMethodInfo.IsFinal )
  125.                sb.Append( "__sealed " );
  126.             if ( m_ThisMethodInfo.IsStatic )
  127.                sb.Append( "static " );
  128.             if ( m_ThisMethodInfo.IsVirtual && !m_ThisMethodInfo.DeclaringType.IsInterface )
  129.                sb.Append( "virtual " );
  130.  
  131.             return sb.ToString();
  132.          }
  133.       }
  134.  
  135.       public String ShortTextDeclaration
  136.       {
  137.          get
  138.          {
  139.             StringBuilder Syntax = new StringBuilder( m_Name );
  140.             Syntax.Append( "(" );
  141.             for ( int i = 0; i < m_Parameters.Count; i++ ) 
  142.             { 
  143.                // if the param is an array, display the element type name and just append *.
  144.                // Note: ClassName in MCLangType returns a Name* for any type that's by definition
  145.                // a pointer.              
  146.                if ( ( (MCLangType)m_Parameters[i] ).ClassName.Equals( "Array*" ) )
  147.                {
  148.                   Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
  149.                   if ( ((MCLangType)m_Parameters[i] ).m_IsByRef )
  150.                      Syntax.Append( "*" );
  151.                }
  152.                else
  153.                { 
  154.                   Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
  155.                   if ( ( (MCLangType)m_Parameters[i] ).m_IsArray )
  156.                      Syntax.Append( "[]" );
  157.                   if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
  158.                      Syntax.Append( "*" );
  159.                }
  160.  
  161.                if ( i != ( m_Parameters.Count - 1 ) )
  162.                   Syntax.Append( ", " );
  163.                else
  164.                {
  165.                   // If there's varargs do the right thing, then finish the syntax block.
  166.                   if ( VarArgs )
  167.                   {
  168.                      Syntax.Append( ", ..." );
  169.                   }
  170.                }
  171.             }
  172.             Syntax.Append( ")" );
  173.             return Syntax.ToString();
  174.          }
  175.       }
  176.  
  177.       public String TextDeclaration
  178.       {
  179.          override get
  180.          {
  181.             StringBuilder Syntax = new StringBuilder( this.Attributes );
  182.             Syntax.Append( m_ReturnLangType.ClassName );
  183.             if ( m_ReturnLangType.m_IsArray )
  184.                Syntax.Append( "[]" );
  185.             if ( m_ReturnLangType.m_IsPointer )
  186.                Syntax.Append( "* " );
  187.             if ( m_ReturnLangType.m_IsByRef )
  188.                Syntax.Append( "* " );
  189.  
  190.             // Review
  191.             Syntax.Append( " " + m_Name + "(" );
  192.             for ( int i = 0; i < m_Parameters.Count; i++ )
  193.             {
  194.                // If the param is an array, display the element type name and just append *.
  195.                // Note: ClassName in MCLangType returns a Name* for any type that's by definition
  196.                // a pointer.
  197.                if ( ( (MCLangType)m_Parameters[i] ).ClassName.Equals( "Array*" ) )
  198.                {
  199.                   Syntax.Append( ((MCLangType)m_Parameters[i] ).ClassName );
  200.                   if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
  201.                      Syntax.Append( "*" );
  202.                }
  203.                else
  204.                { 
  205.                   Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
  206.                   if ( ( (MCLangType)m_Parameters[i] ).m_IsArray )
  207.                      Syntax.Append( "[]" );
  208.  
  209.                   if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
  210.                      Syntax.Append( "&" );
  211.                }
  212.                Syntax.Append( " " + ( (MCLangType)m_Parameters[i] ).ParamVarName );
  213.  
  214.                if ( i != ( m_Parameters.Count - 1 ) )
  215.                   Syntax.Append( ", " );
  216.                else
  217.                {
  218.                   // If there's varargs do the right thing, then finish the syntax block
  219.                   if ( VarArgs )
  220.                   {
  221.                      Syntax.Append( ", ..." );
  222.                   }
  223.                }
  224.             } 
  225.             Syntax.Append( ")" );
  226.             if ( m_ThisMethodInfo.IsAbstract && !m_ThisMethodInfo.DeclaringType.IsInterface )
  227.             {
  228.                Syntax.Append( " = 0" );
  229.             }
  230.             Syntax.Append( ";" );
  231.             return Syntax.ToString();
  232.          }
  233.       }
  234.  
  235.       public String HTMLDeclaration
  236.       {
  237.          override get
  238.          {
  239.             StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
  240.             Syntax.Append( "<span style=\"color:red\">[MC++]</span><BR><B>" );
  241.             Syntax.Append( this.Attributes );
  242.             Syntax.Append( m_ReturnLangType.ClassName );
  243.             if ( m_ReturnLangType.m_IsArray )
  244.                Syntax.Append( "[]" );
  245.             if ( m_ReturnLangType.m_IsPointer )
  246.                Syntax.Append( "* " );
  247.             if ( m_ReturnLangType.m_IsByRef )
  248.                Syntax.Append( "* " );
  249.  
  250.             // Review
  251.             Syntax.Append( " " + m_Name + "(" );
  252.             for ( int i = 0; i < m_Parameters.Count; i++ ) 
  253.             { 
  254.                Syntax.Append( "<BR>    " );
  255.  
  256.                // if the param is an array, display the element type name and just append *.
  257.                // Note: ClassName in MCLangType returns a Name* for any type that's by definition
  258.                // a pointer.              
  259.                if ( ( (MCLangType)m_Parameters[i] ).ClassName.Equals( "Array*" ) )
  260.                {
  261.                   Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
  262.                   if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
  263.                      Syntax.Append( "*" );
  264.                }
  265.                else
  266.                { 
  267.                   Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
  268.                   if ( ( (MCLangType)m_Parameters[i] ).m_IsArray )
  269.                      Syntax.Append( "[]" );
  270.                   if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
  271.                      Syntax.Append( "*" );
  272.                }
  273.  
  274.                Syntax.Append( " <I>" + ( (MCLangType)m_Parameters[i] ).ParamVarName + "</I>" );
  275.  
  276.                // Writes the comma, or the last break
  277.                if ( i != ( m_Parameters.Count - 1 ) )
  278.                   Syntax.Append( ", " );
  279.                else
  280.                {
  281.                   // If there's varargs do the right thing, then finish the syntax block
  282.                   if ( VarArgs )
  283.                   {
  284.                      Syntax.Append( ", " );
  285.                      Syntax.Append( "<BR/>    ..." );
  286.                   }
  287.  
  288.                   // Else finish the syntax block
  289.                   Syntax.Append( "<BR/>" );
  290.                }
  291.             } 
  292.             Syntax.Append( ")" );
  293.             if ( m_ThisMethodInfo.IsAbstract && !m_ThisMethodInfo.DeclaringType.IsInterface )
  294.             {
  295.                Syntax.Append( " = 0" );
  296.             }
  297.             Syntax.Append( ";" );
  298.             Syntax.Append( "</B></PRE>" );
  299.             return Syntax.ToString();
  300.          }
  301.       }
  302.  
  303.       public override bool IsMethod()
  304.       {
  305.          return true;
  306.       }       
  307.    }
  308.  
  309. } // namespace LangUtil
  310.