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

  1. /*=====================================================================
  2.   File:      LangPropertyInfo.cs
  3.  
  4.   Summary:   Implements or declares all the functionality common to 
  5.          subclasses of this class.
  6.  
  7. ---------------------------------------------------------------------
  8.   This file is part of the Microsoft NGWS SDK Code Samples.
  9.  
  10.   Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  11.  
  12. This source code is intended only as a supplement to Microsoft
  13. Development Tools and/or on-line documentation.  See these other
  14. materials for detailed information regarding Microsoft code samples.
  15.  
  16. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  17. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  19. PARTICULAR PURPOSE.
  20. =====================================================================*/
  21.  
  22. namespace LangUtil {
  23.  
  24. using System;
  25. using System.Text;
  26. using System.Reflection;
  27. using System.Collections;
  28.  
  29.    public abstract class LangPropertyInfo : LangMember
  30.    {
  31.       protected String m_Name = String.Empty;
  32.       protected PropertyInfo m_ThisPropertyInfo = null;
  33.       
  34.       // Must implement these on the children
  35.       protected CSharpLangType m_GetSetType = null;
  36.       protected CSharpMethodInfo m_Getter = null;
  37.       protected CSharpMethodInfo m_Setter = null;
  38.  
  39.       protected ArrayList m_Parameters = new ArrayList();
  40.       protected String m_Access = String.Empty;
  41.  
  42.       public LangPropertyInfo( PropertyInfo myProperty )
  43.       {
  44.          try
  45.          {
  46.             if ( myProperty == null )
  47.                throw new NullReferenceException( "The PropertyInfo passed to the constructor is null." );
  48.  
  49.             // Obtain Property Name and Type
  50.             m_Name = myProperty.Name;
  51.             m_ThisPropertyInfo = myProperty;
  52.  
  53.             // Obtain Getter and Setter
  54.             if ( myProperty.CanRead )
  55.             {
  56.                m_Getter = new CSharpMethodInfo( myProperty.GetGetMethod( true ) );
  57.             }
  58.             if ( myProperty.CanWrite )
  59.             {
  60.                m_Setter = new CSharpMethodInfo( myProperty.GetSetMethod( true ) );
  61.             }
  62.  
  63.  
  64.             CSharpMethodInfo obj;
  65.                
  66.             if ( myProperty.CanRead )
  67.             {
  68.                ParameterInfo[] tmpArray = myProperty.GetGetMethod( true ).GetParameters();
  69.  
  70.                // SEE Member.aspx, THE LAST ARG IS RETURN TYPE OF GETTER
  71.                for ( int i = 0; i < tmpArray.Length; i++ )
  72.                {
  73.                   m_Parameters.Add( new CSharpLangType( tmpArray[i] ) );
  74.                }     
  75.  
  76.                m_GetSetType = new CSharpLangType( myProperty.GetGetMethod( true ).ReturnType );
  77.                obj = m_Getter;
  78.             }
  79.             else if ( myProperty.CanWrite )
  80.             {
  81.                ParameterInfo[] tmpArray = myProperty.GetSetMethod( true ).GetParameters();
  82.  
  83.                // SEE Member.aspx, THE LAST ARG IS NOT AN ARG OF GETTER
  84.                for ( int i = 0; i < tmpArray.Length - 1; i++ )
  85.                {
  86.                   m_Parameters.Add( new CSharpLangType( tmpArray[i] ) );
  87.                }     
  88.  
  89.                // THE LAST ARG HAS SAME TYPE AS THE TYPE OF THIS PROPERTY
  90.                m_GetSetType = new CSharpLangType( tmpArray[tmpArray.Length - 1] );
  91.                obj = m_Setter;
  92.             }
  93.             else
  94.             {
  95.                 ;
  96.             }
  97.  
  98.          }
  99.          catch ( Exception Ex ) 
  100.          {
  101.             throw new Exception( Ex.GetType().Name + " error in property constructor.", Ex );
  102.          }
  103.       }
  104.  
  105.       public ArrayList Parameters
  106.       {
  107.          get { return m_Parameters; }
  108.       }
  109.  
  110.       public String Name 
  111.       { 
  112.          override get { return m_Name; }
  113.       }
  114.  
  115.       /*
  116.       //    This property must be implemented on subclasses to return a LangType for that
  117.       //    language that the current class represents to avoid callers having to 
  118.             public <LanguageName>LangType GetSetLangType{
  119.                get{
  120.                   return m_GetSetType;
  121.                }
  122.             }
  123.       */
  124.  
  125.       public Type ThisType
  126.       {
  127.          override get { return m_ThisPropertyInfo.ReflectedType; }
  128.       }
  129.  
  130.       public String TextDeclaration
  131.       {
  132.          override get
  133.          {
  134.             StringBuilder Syntax = new StringBuilder( Access );
  135.             Syntax.Append( m_GetSetType.ClassName + " " );
  136.             if ( m_Name.Equals( "Item" ) )
  137.                Syntax.Append( "this" );
  138.             else
  139.                Syntax.Append( m_Name );
  140.             if ( m_Parameters.Count > 0 ) 
  141.                Syntax.Append( "[" );
  142.             for ( int j = 0; j < m_Parameters.Count; j++ ) 
  143.             { 
  144.                Syntax.Append( ( (CSharpLangType)m_Parameters[j] ).ClassName );
  145.                Syntax.Append( " " + ( (CSharpLangType)m_Parameters[j] ).ParamVarName );
  146.                if ( j != ( m_Parameters.Count - 1 ) ) 
  147.                { 
  148.                   Syntax.Append( ", " ); 
  149.                } 
  150.                else 
  151.                { 
  152.                   Syntax.Append( "]" );
  153.                }
  154.             } 
  155.             Syntax.Append( " {" );
  156.             if ( m_ThisPropertyInfo.CanRead ) 
  157.             {
  158.                if ( m_Getter.Attributes.IndexOf( "override" ) > -1 )
  159.                   Syntax.Append( "override " );
  160.                else if ( m_Getter.Attributes.IndexOf( "abstract" ) > -1 )
  161.                   Syntax.Append( "abstract " );
  162.                else if ( m_Getter.Attributes.IndexOf( "virtual" ) > -1 )
  163.                   Syntax.Append( "virtual " );
  164.                Syntax.Append( "get;" );
  165.             }
  166.             if ( m_ThisPropertyInfo.CanWrite )
  167.             {
  168.                if ( m_Setter.Attributes.IndexOf( "override" ) > -1 )
  169.                   Syntax.Append( "override " );
  170.                else if ( m_Setter.Attributes.IndexOf( "abstract" ) > -1 )
  171.                   Syntax.Append( "abstract " );
  172.                else if ( m_Setter.Attributes.IndexOf( "virtual" ) > -1 )
  173.                   Syntax.Append( "virtual " );
  174.                Syntax.Append( "set;" );
  175.             }
  176.             Syntax.Append( "}" );
  177.             return Syntax.ToString();
  178.          }
  179.       }
  180.  
  181.       public string HTMLDeclaration
  182.       {
  183.          override get
  184.          {
  185.             StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
  186.             Syntax.Append( "<span style=\"color:red\">[CSharp]</span><BR/><B>" );
  187.             Syntax.Append( Access );
  188.  
  189.             Syntax.Append( m_GetSetType.ClassName + " " );
  190.             if ( m_Name.Equals( "Item" ) )
  191.                Syntax.Append( "this" );
  192.             else
  193.                Syntax.Append( m_Name );
  194.             if ( m_Parameters.Count > 0 ) 
  195.                Syntax.Append( "[" );
  196.             for ( int j = 0; j < m_Parameters.Count; j++ ) 
  197.             { 
  198.                Syntax.Append( ( (CSharpLangType)m_Parameters[j] ).ClassName );
  199.                // write the variable name.
  200.                Syntax.Append( " <I>" + ( (CSharpLangType)m_Parameters[j] ).ParamVarName + "</I>" );
  201.                if ( j != ( m_Parameters.Count - 1 ) ) 
  202.                { 
  203.                   Syntax.Append( ", " ); 
  204.                } 
  205.                else 
  206.                { 
  207.                   Syntax.Append( "]" );
  208.                }
  209.             } 
  210.             Syntax.Append( " {<BR/>    " );
  211.             if ( m_ThisPropertyInfo.CanRead ) 
  212.             {
  213.                if ( m_Getter.Attributes.IndexOf( "override" ) > -1 )
  214.                   Syntax.Append( "override " );
  215.                else if ( m_Getter.Attributes.IndexOf( "abstract" ) > -1 )
  216.                   Syntax.Append( "abstract " );
  217.                else if ( m_Getter.Attributes.IndexOf( "virtual" ) > -1 )
  218.                   Syntax.Append( "virtual " );
  219.                Syntax.Append( "get;" );
  220.             }
  221.             if ( m_ThisPropertyInfo.CanWrite )
  222.             {
  223.                Syntax.Append( "<BR/>    " );
  224.                if ( m_Setter.Attributes.IndexOf( "override" ) > -1 )
  225.                   Syntax.Append( "override " );
  226.                else if ( m_Setter.Attributes.IndexOf( "abstract" ) > -1 )
  227.                   Syntax.Append( "abstract " );
  228.                else if (m_Setter.Attributes.IndexOf( "virtual" ) > -1 )
  229.                   Syntax.Append( "virtual " );
  230.                Syntax.Append( "set;" );
  231.             }
  232.             Syntax.Append( "<BR/>}</B></PRE>" );
  233.             return Syntax.ToString();
  234.          }
  235.       }
  236.  
  237.       // Access must be implemented by children. It returns a string containing the 
  238.       // level of access exposed by the accessor methods for this property 
  239.       // IFF the two levels are the same. If they are not, it throws an exception. 
  240.  
  241.       protected String Access
  242.       {
  243.          abstract get;
  244.       }
  245.  
  246.       public String Attributes
  247.       {
  248.          override get { return this.Access; }
  249.        }
  250.       
  251.        public override bool IsProperty()
  252.        {
  253.          return true;
  254.        }
  255.    }
  256.  
  257. } // namespace LangUtil
  258.