home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: MCMethodInfo.cs
-
- Summary: Brief summary of the file contents and purpose.
-
- ---------------------------------------------------------------------
- This file is part of the Microsoft NGWS SDK Code Samples.
-
- Copyright (C) 2000 Microsoft Corporation. All rights reserved.
-
- This source code is intended only as a supplement to Microsoft
- Development Tools and/or on-line documentation. See these other
- materials for detailed information regarding Microsoft code samples.
-
- THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
- KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
- PARTICULAR PURPOSE.
- =====================================================================*/
-
- namespace LangUtil {
-
- using System;
- using System.Text;
- using System.Reflection;
- using System.Collections;
-
- public class MCMethodInfo : LangMember
- {
- internal String m_Name = String.Empty;
- internal MethodInfo m_ThisMethodInfo = null;
- internal MCLangType m_ReturnLangType = null;
- internal ArrayList m_Parameters = null;
-
- public MCMethodInfo( MethodInfo myMethod )
- {
- if ( myMethod == null )
- throw new NullReferenceException( "The MethodInfo passed to the constructor is null." );
-
- // Determine method name and return type
- m_Name = myMethod.Name;
- m_ThisMethodInfo = myMethod;
- m_ReturnLangType = new MCLangType( myMethod.ReturnType );
-
- // Obtain parameter details
- ParameterInfo[] myParams = myMethod.GetParameters();
-
- // Set arraylist size
- m_Parameters = new ArrayList(myParams.Length);
-
- // Determine whether method can accept variable-length arguments
- if ( myMethod.CallingConvention == CallingConventions.VarArgs )
- m_VarArgs = true;
-
- for ( int x = 0; x < myParams.Length; x++ )
- {
- m_Parameters.Add( new MCLangType( myParams[x] ) );
- }
- }
-
- public String Name
- {
- override get { return m_Name; }
- }
-
- public bool IsOverloaded
- {
- get
- {
- MethodInfo[] allMethods = m_ThisMethodInfo.DeclaringType.GetMethods( BindingFlags.NonPublic );
- int cnt = 0;
- for ( int i = 0; i < allMethods.Length; i++ )
- {
- if ( !allMethods[i].IsPrivate && allMethods[i].Name.Equals( m_Name ) )
- cnt++;
- }
- if ( cnt > 1 )
- return true;
- else
- return false;
- }
- }
-
- public MethodInfo ThisMethodInfo
- {
- get { return m_ThisMethodInfo; }
- }
-
- public MCLangType ReturnLangType
- {
- get { return m_ReturnLangType; }
- }
-
- public ArrayList Parameters
- {
- get { return m_Parameters; }
- }
-
- public Type ThisType
- {
- override get { return m_ThisMethodInfo.ReflectedType; }
-
- }
-
- public string Attributes
- {
- override get
- {
- StringBuilder sb = new StringBuilder();
-
- if ( m_ThisMethodInfo.IsPublic )
- sb.Append( "public: " );
- else if ( m_ThisMethodInfo.IsPrivate )
- sb.Append( "private: " );
- else if ( m_ThisMethodInfo.IsFamily )
- sb.Append( "protected: " );
- else if ( m_ThisMethodInfo.IsAssembly )
- sb.Append( "Assembly: " );
- else if ( m_ThisMethodInfo.IsFamilyAndAssembly )
- sb.Append( "FamilyAndAssembly: " );
- else if ( m_ThisMethodInfo.IsFamilyOrAssembly )
- sb.Append( "FamilyOrAssembly: " );
-
- if ( m_ThisMethodInfo.IsFinal )
- sb.Append( "__sealed " );
- if ( m_ThisMethodInfo.IsStatic )
- sb.Append( "static " );
- if ( m_ThisMethodInfo.IsVirtual && !m_ThisMethodInfo.DeclaringType.IsInterface )
- sb.Append( "virtual " );
-
- return sb.ToString();
- }
- }
-
- public String ShortTextDeclaration
- {
- get
- {
- StringBuilder Syntax = new StringBuilder( m_Name );
- Syntax.Append( "(" );
- for ( int i = 0; i < m_Parameters.Count; i++ )
- {
- // if the param is an array, display the element type name and just append *.
- // Note: ClassName in MCLangType returns a Name* for any type that's by definition
- // a pointer.
- if ( ( (MCLangType)m_Parameters[i] ).ClassName.Equals( "Array*" ) )
- {
- Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
- if ( ((MCLangType)m_Parameters[i] ).m_IsByRef )
- Syntax.Append( "*" );
- }
- else
- {
- Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
- if ( ( (MCLangType)m_Parameters[i] ).m_IsArray )
- Syntax.Append( "[]" );
- if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
- Syntax.Append( "*" );
- }
-
- if ( i != ( m_Parameters.Count - 1 ) )
- Syntax.Append( ", " );
- else
- {
- // If there's varargs do the right thing, then finish the syntax block.
- if ( VarArgs )
- {
- Syntax.Append( ", ..." );
- }
- }
- }
- Syntax.Append( ")" );
- return Syntax.ToString();
- }
- }
-
- public String TextDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder( this.Attributes );
- Syntax.Append( m_ReturnLangType.ClassName );
- if ( m_ReturnLangType.m_IsArray )
- Syntax.Append( "[]" );
- if ( m_ReturnLangType.m_IsPointer )
- Syntax.Append( "* " );
- if ( m_ReturnLangType.m_IsByRef )
- Syntax.Append( "* " );
-
- // Review
- Syntax.Append( " " + m_Name + "(" );
- for ( int i = 0; i < m_Parameters.Count; i++ )
- {
- // If the param is an array, display the element type name and just append *.
- // Note: ClassName in MCLangType returns a Name* for any type that's by definition
- // a pointer.
- if ( ( (MCLangType)m_Parameters[i] ).ClassName.Equals( "Array*" ) )
- {
- Syntax.Append( ((MCLangType)m_Parameters[i] ).ClassName );
- if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
- Syntax.Append( "*" );
- }
- else
- {
- Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
- if ( ( (MCLangType)m_Parameters[i] ).m_IsArray )
- Syntax.Append( "[]" );
-
- if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
- Syntax.Append( "&" );
- }
- Syntax.Append( " " + ( (MCLangType)m_Parameters[i] ).ParamVarName );
-
- if ( i != ( m_Parameters.Count - 1 ) )
- Syntax.Append( ", " );
- else
- {
- // If there's varargs do the right thing, then finish the syntax block
- if ( VarArgs )
- {
- Syntax.Append( ", ..." );
- }
- }
- }
- Syntax.Append( ")" );
- if ( m_ThisMethodInfo.IsAbstract && !m_ThisMethodInfo.DeclaringType.IsInterface )
- {
- Syntax.Append( " = 0" );
- }
- Syntax.Append( ";" );
- return Syntax.ToString();
- }
- }
-
- public String HTMLDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
- Syntax.Append( "<span style=\"color:red\">[MC++]</span><BR><B>" );
- Syntax.Append( this.Attributes );
- Syntax.Append( m_ReturnLangType.ClassName );
- if ( m_ReturnLangType.m_IsArray )
- Syntax.Append( "[]" );
- if ( m_ReturnLangType.m_IsPointer )
- Syntax.Append( "* " );
- if ( m_ReturnLangType.m_IsByRef )
- Syntax.Append( "* " );
-
- // Review
- Syntax.Append( " " + m_Name + "(" );
- for ( int i = 0; i < m_Parameters.Count; i++ )
- {
- Syntax.Append( "<BR> " );
-
- // if the param is an array, display the element type name and just append *.
- // Note: ClassName in MCLangType returns a Name* for any type that's by definition
- // a pointer.
- if ( ( (MCLangType)m_Parameters[i] ).ClassName.Equals( "Array*" ) )
- {
- Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
- if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
- Syntax.Append( "*" );
- }
- else
- {
- Syntax.Append( ( (MCLangType)m_Parameters[i] ).ClassName );
- if ( ( (MCLangType)m_Parameters[i] ).m_IsArray )
- Syntax.Append( "[]" );
- if ( ( (MCLangType)m_Parameters[i] ).m_IsByRef )
- Syntax.Append( "*" );
- }
-
- Syntax.Append( " <I>" + ( (MCLangType)m_Parameters[i] ).ParamVarName + "</I>" );
-
- // Writes the comma, or the last break
- if ( i != ( m_Parameters.Count - 1 ) )
- Syntax.Append( ", " );
- else
- {
- // If there's varargs do the right thing, then finish the syntax block
- if ( VarArgs )
- {
- Syntax.Append( ", " );
- Syntax.Append( "<BR/> ..." );
- }
-
- // Else finish the syntax block
- Syntax.Append( "<BR/>" );
- }
- }
- Syntax.Append( ")" );
- if ( m_ThisMethodInfo.IsAbstract && !m_ThisMethodInfo.DeclaringType.IsInterface )
- {
- Syntax.Append( " = 0" );
- }
- Syntax.Append( ";" );
- Syntax.Append( "</B></PRE>" );
- return Syntax.ToString();
- }
- }
-
- public override bool IsMethod()
- {
- return true;
- }
- }
-
- } // namespace LangUtil
-