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

  1. /*=====================================================================
  2.   File:      NativeMethodInfo.cs
  3.  
  4.   Summary:   For language-neutral reflection information.
  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 NativeMethodInfo : LangMember
  29.    {
  30.       internal String m_Name = String.Empty;
  31.       internal MethodInfo m_ThisMethodInfo = null;
  32.       internal NativeLangType m_ReturnLangType = null;
  33.       internal ArrayList m_Parameters = null;
  34.  
  35.       public NativeMethodInfo( 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 NativeLangType( myMethod.ReturnType );
  44.  
  45.          // Obtain parameter details
  46.          ParameterInfo [] myParams = myMethod.GetParameters();
  47.  
  48.          // Determine whether method can accept variable-length arguments
  49.          if ( myMethod.CallingConvention == CallingConventions.VarArgs )
  50.             m_VarArgs = true;
  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 NativeLangType( myParams[x] ) );
  58.          }   
  59.       }
  60.  
  61.       public bool IsOverloaded
  62.       {
  63.          get
  64.          {
  65.             MethodInfo[] allMethods = m_ThisMethodInfo.DeclaringType.GetMethods( BindingFlags.LookupAll );
  66.             int cnt = 0;
  67.             for ( int i = 0; i < allMethods.Length; i++ )
  68.             {
  69.                if ( !allMethods[i].IsPrivate && allMethods[i].Name.Equals( m_Name ) ) 
  70.                   cnt++;
  71.             }
  72.             if ( cnt > 1 ) 
  73.                return true;
  74.             else 
  75.                return false;
  76.          }
  77.       }
  78.  
  79.       public Type ThisType
  80.       {
  81.          override get { return m_ThisMethodInfo.ReflectedType; }
  82.       }
  83.  
  84.       public String Name
  85.       {
  86.          override get { return m_Name; }
  87.       }
  88.  
  89.       public MethodInfo ThisMethodInfo
  90.       {
  91.          get { return m_ThisMethodInfo; }
  92.       }
  93.  
  94.       public NativeLangType ReturnLangType
  95.       {
  96.          get { return m_ReturnLangType; }
  97.       }
  98.  
  99.       public ArrayList Parameters
  100.       {
  101.          get { return m_Parameters; }
  102.       }
  103.  
  104.       public VisualBasicMethodInfo VisualBasicLangObject
  105.       {
  106.          get { return new VisualBasicMethodInfo( m_ThisMethodInfo ); }
  107.       }
  108.  
  109.       public CSharpMethodInfo CSharpLangObject
  110.       {
  111.          get { return new CSharpMethodInfo( m_ThisMethodInfo ); }
  112.       }
  113.  
  114.       public MCMethodInfo MCLangObject
  115.       {
  116.          get { return new MCMethodInfo( m_ThisMethodInfo ); }
  117.       }
  118.  
  119.       public string Attributes
  120.       {
  121.          override get 
  122.          {
  123.             throw new Exception( "NativeMethodInfo.NativeAttributes has not yet been implemented." );
  124.       
  125.          }
  126.       }
  127.  
  128.       public String TextDeclaration
  129.       {
  130.          override get
  131.          {
  132.             StringBuilder Syntax = new StringBuilder( this.Attributes );
  133.  
  134.             Syntax.Append( m_ReturnLangType.ClassName + " " + m_Name + "(" );
  135.  
  136.             for ( int i = 0; i < m_Parameters.Count; i++ ) 
  137.             { 
  138.                if ( ( (NativeLangType)m_Parameters[i] ).IsByRef )
  139.                   Syntax.Append( "ref " );
  140.                Syntax.Append( ( (NativeLangType)m_Parameters[i] ).ClassName );
  141.                Syntax.Append( " " );
  142.                Syntax.Append( ( (NativeLangType)m_Parameters[i] ).ParamVarName );
  143.  
  144.                // Writes the comma, or the last break
  145.                if ( i != ( m_Parameters.Count - 1 ) )
  146.                   Syntax.Append( ", " );
  147.                else
  148.                {
  149.                   // If there's varargs
  150.                   if ( VarArgs )
  151.                   {
  152.                      Syntax.Append( ", " );
  153.                      Syntax.Append( "..." );
  154.                   }
  155.                }
  156.             } 
  157.             Syntax.Append( ");" );
  158.             return Syntax.ToString();
  159.          }
  160.       }
  161.  
  162.       public String HTMLDeclaration
  163.       {
  164.          override get
  165.          {
  166.             StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
  167.             Syntax.Append( "<span style=\"color:red\">[CSharp]</span><BR><B>" );
  168.             Syntax.Append( this.Attributes );
  169.  
  170.             Syntax.Append( ( (NativeLangType)m_ReturnLangType ).ClassName + " " + m_Name + "(" );
  171.  
  172.             for ( int i = 0; i < m_Parameters.Count; i++ ) 
  173.             { 
  174.                Syntax.Append( "<BR>    " );
  175.                if ( ( (NativeLangType)m_Parameters[i] ).IsByRef )
  176.                   Syntax.Append( "ref " );
  177.                Syntax.Append( ( (NativeLangType)m_Parameters[i] ).ClassName );
  178.                Syntax.Append( " <I>" + ( (NativeLangType)m_Parameters[i] ).ParamVarName + "</I>" );
  179.  
  180.                // Writes the comma, or the last break
  181.                if ( i != ( m_Parameters.Count - 1 ) )
  182.                   Syntax.Append( ", " );
  183.                else
  184.                {
  185.                   // If there's varargs 
  186.                   if ( VarArgs )
  187.                   {
  188.                      Syntax.Append( ", <BR/>    ..." );
  189.                   }
  190.                   Syntax.Append( "<BR/>" );
  191.                }
  192.             } 
  193.             Syntax.Append( ");</B></PRE>" );
  194.             return Syntax.ToString();
  195.          }
  196.       }
  197.  
  198.       public override bool IsMethod()
  199.       {
  200.          return true;
  201.       }       
  202.    }
  203.  
  204. } // namespace LangUtil
  205.