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

  1. /*=====================================================================
  2.   File:     CSharpPropertyInfo.cs
  3.  
  4.   Summary:  Extends LangPropertInfo to provide a CSharp-specific version
  5.          of reflection for ease of use in a CSharp-based environment.
  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 class CSharpPropertyInfo : LangPropertyInfo
  30.    {
  31.       public CSharpPropertyInfo( PropertyInfo myProperty ) : base( myProperty )
  32.       {
  33.       }
  34.  
  35.       public CSharpMethodInfo Getter
  36.       {
  37.          get
  38.          {
  39.             try
  40.             {
  41.                if ( m_ThisPropertyInfo.CanRead )
  42.                {
  43.                   return new CSharpMethodInfo( m_ThisPropertyInfo.GetGetMethod( true ) );
  44.                }
  45.                else return null;
  46.             }
  47.             catch
  48.             {
  49.                throw new NullReferenceException( "Although " 
  50.                         + this.ThisType.Name
  51.                         + "."
  52.                         + m_ThisPropertyInfo.Name
  53.                         + " says it is CanRead,"
  54.                         + " GetGetMethod(true) returns a null MethodInfo." );
  55.             }
  56.          }
  57.       }
  58.  
  59.       public CSharpMethodInfo Setter
  60.       {
  61.          get
  62.          {
  63.             try
  64.             {
  65.                if ( m_ThisPropertyInfo.CanWrite )
  66.                {
  67.                   return new CSharpMethodInfo( m_ThisPropertyInfo.GetSetMethod( true ) );
  68.                }
  69.                else return null;
  70.             }
  71.             catch
  72.             {
  73.                throw new NullReferenceException( "Although " 
  74.                         + this.ThisType.Name
  75.                         + "."
  76.                         + m_ThisPropertyInfo.Name
  77.                         + " says it is CanWrite,"
  78.                         + " GetSetMethod(true) returns a null MethodInfo." );
  79.             }
  80.          }
  81.       }
  82.  
  83.       public CSharpLangType GetSetLangType
  84.       {
  85.          get
  86.          {
  87.             try
  88.             {
  89.                if ( m_ThisPropertyInfo.CanWrite )
  90.                {
  91.                   return new CSharpLangType( m_ThisPropertyInfo.GetGetMethod( true ).ReturnType );
  92.                }
  93.                else return null;
  94.             }
  95.             catch
  96.             {
  97.                throw new NullReferenceException( "Although " 
  98.                         + this.ThisType.Name
  99.                         + "."
  100.                         + m_ThisPropertyInfo.Name
  101.                         + " says it is CanWrite,"
  102.                         + " GetSetMethod(true) returns a null MethodInfo." );
  103.             }
  104.          }
  105.       }
  106.  
  107.       public String TextDeclaration
  108.       {         
  109.          override get
  110.          {
  111.             StringBuilder Syntax = new StringBuilder( Access );
  112.  
  113.             if ( m_ThisPropertyInfo.CanRead )
  114.             {
  115.                if ( m_Getter.Attributes.IndexOf( "override" ) > -1 )
  116.                   Syntax.Append( "override " );
  117.                else if ( m_Getter.Attributes.IndexOf( "abstract" ) > -1 )
  118.                   Syntax.Append( "abstract " );
  119.                else if ( m_Getter.Attributes.IndexOf( "virtual" ) > -1 )
  120.                   Syntax.Append( "virtual " );
  121.             }
  122.             else if ( m_ThisPropertyInfo.CanWrite )
  123.             {
  124.                if ( m_Setter.Attributes.IndexOf( "override" ) > -1 )
  125.                   Syntax.Append( "override " );
  126.                else if ( m_Setter.Attributes.IndexOf( "abstract" ) > -1 )
  127.                   Syntax.Append( "abstract " );
  128.                else if ( m_Setter.Attributes.IndexOf( "virtual" ) > -1 )
  129.                   Syntax.Append( "virtual " );   
  130.             }
  131.                         
  132.             if ( m_GetSetType.m_IsByRef )
  133.                Syntax.Append( "ref " );
  134.             Syntax.Append( m_GetSetType.ClassName );
  135.             if ( m_GetSetType.m_IsArray )
  136.                Syntax.Append( "[]" );
  137.  
  138.             Syntax.Append( " " );
  139.  
  140.             if ( m_Name.Equals( "Item" ) )
  141.                Syntax.Append( "this" );
  142.             else
  143.                Syntax.Append( m_Name );
  144.  
  145.             if ( m_Parameters.Count > 0 )
  146.                Syntax.Append( "[" );
  147.  
  148.             for ( int j = 0; j < m_Parameters.Count; j++ ) 
  149.             { 
  150.                if ( ( (CSharpLangType)m_Parameters[j] ).m_IsByRef )
  151.                   Syntax.Append( "ref " );
  152.                Syntax.Append( ( (CSharpLangType)m_Parameters[j] ).ClassName );
  153.                if ( ((CSharpLangType)m_Parameters[j] ).m_IsArray )
  154.                   Syntax.Append( "[]" );
  155.                /*             if (((CSharpLangType)m_Parameters[j]).m_IsPointer)
  156.                resolve              Syntax.Append("*");
  157.                */
  158.                Syntax.Append( " " + ( (CSharpLangType)m_Parameters[j]).ParamVarName );
  159.  
  160.                if ( j != ( m_Parameters.Count - 1 ) ) 
  161.                { 
  162.                   Syntax.Append( ", " );
  163.                } 
  164.                else 
  165.                {
  166.                   Syntax.Append( "]" );
  167.                }
  168.             } 
  169.  
  170.             Syntax.Append( " {" );
  171.             if ( m_ThisPropertyInfo.CanRead )
  172.                Syntax.Append( "get;" );
  173.             if ( m_ThisPropertyInfo.CanWrite )
  174.                Syntax.Append( " set;" );
  175.             Syntax.Append( "}" );
  176.  
  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.             if ( m_GetSetType.m_IsByRef )
  190.                Syntax.Append( "ref " );
  191.             Syntax.Append( m_GetSetType.ClassName );
  192.             if ( m_GetSetType.m_IsArray )
  193.                Syntax.Append( "[]" );
  194.  
  195.             Syntax.Append( " " );
  196.             if ( m_Name.Equals( "Item" ) )
  197.                Syntax.Append( "this" );
  198.             else
  199.                Syntax.Append( m_Name );
  200.             if ( m_Parameters.Count > 0 ) 
  201.                Syntax.Append( "[" );
  202.             for ( int j = 0; j < m_Parameters.Count; j++ )
  203.             { 
  204.                if ( ( (CSharpLangType)m_Parameters[j] ).m_IsByRef )
  205.                   Syntax.Append( "ref " );
  206.                Syntax.Append( ( (CSharpLangType)m_Parameters[j] ).ClassName );
  207.                if ( ( (CSharpLangType)m_Parameters[j] ).m_IsArray )
  208.                   Syntax.Append( "[]" );
  209.                /*             if (((CSharpLangType)m_Parameters[j]).m_IsPointer)
  210.                resolve              Syntax.Append("*");
  211.                */
  212.                // write the variable name.
  213.                Syntax.Append( " <I>" + ( (CSharpLangType)m_Parameters[j] ).ParamVarName + "</I>" );
  214.                if ( j != ( m_Parameters.Count - 1 ) )
  215.                { 
  216.                   Syntax.Append( ", " ); 
  217.                } 
  218.                else 
  219.                { 
  220.                   Syntax.Append( "]" );
  221.                }
  222.             } 
  223.             Syntax.Append( " {<BR/>    " );
  224.             if ( m_ThisPropertyInfo.CanRead ) 
  225.             {
  226.                if ( m_Getter.Attributes.IndexOf( "override" ) > -1 )
  227.                   Syntax.Append( "override " );
  228.                else if ( m_Getter.Attributes.IndexOf( "abstract" ) > -1 )
  229.                   Syntax.Append( "abstract " );
  230.                else if ( m_Getter.Attributes.IndexOf( "virtual" ) > -1 )
  231.                   Syntax.Append( "virtual " );
  232.                Syntax.Append( "get;" );
  233.             }
  234.             if ( m_ThisPropertyInfo.CanWrite )
  235.             {
  236.                Syntax.Append( "<BR/>    " );
  237.                if ( m_Setter.Attributes.IndexOf( "override" ) > -1 )
  238.                   Syntax.Append( "override " );
  239.                else if ( m_Setter.Attributes.IndexOf( "abstract" ) > -1 )
  240.                   Syntax.Append( "abstract " );
  241.                else if ( m_Setter.Attributes.IndexOf( "virtual" ) > -1 )
  242.                   Syntax.Append( "virtual " );
  243.                Syntax.Append( "set;" );
  244.             }
  245.             Syntax.Append( "<BR/>}</B></PRE>" );
  246.             return Syntax.ToString();
  247.          }
  248.       }
  249.  
  250.  
  251.       // Access returns a string containing the level of access exposed by the accessor methods for this property 
  252.       // IFF the two levels are the same. If they are not, it throws an exception. 
  253.  
  254.       protected String Access
  255.       {
  256.          override get
  257.          {
  258.             String m_Access = String.Empty;
  259.             if ( m_ThisPropertyInfo.CanRead && m_ThisPropertyInfo.CanWrite )
  260.             {
  261.                if ( m_Getter.ThisMethodInfo.IsPublic && m_Setter.ThisMethodInfo.IsPublic )
  262.                   m_Access = "public ";
  263.                else if ( m_Getter.ThisMethodInfo.IsFamily && m_Setter.ThisMethodInfo.IsFamily )
  264.                   m_Access = "protected ";
  265.                else if ( m_Getter.ThisMethodInfo.IsPrivate && m_Setter.ThisMethodInfo.IsPrivate )
  266.                   m_Access = "private ";
  267.                else if ( m_Getter.ThisMethodInfo.IsAssembly && m_Setter.ThisMethodInfo.IsAssembly )
  268.                   m_Access = "internal ";
  269.                else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly && m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
  270.                   m_Access = "FamilyOrAssembly ";
  271.                else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly && m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
  272.                   m_Access = "FamilyAndAssembly ";
  273.                else
  274.                {
  275.                   throw new Exception( "Accessor-methods claim to agree but metadata disagrees with this. On property: "
  276.                         + m_ThisPropertyInfo.ReflectedType.FullName + "."
  277.                         + m_ThisPropertyInfo.Name );
  278.                }
  279.             }
  280.             else if ( m_ThisPropertyInfo.CanRead )
  281.             {
  282.                if ( m_Getter.ThisMethodInfo.IsPublic )
  283.                   m_Access = "public ";
  284.                else if ( m_Getter.ThisMethodInfo.IsFamily )
  285.                   m_Access = "protected ";
  286.                else if ( m_Getter.ThisMethodInfo.IsPrivate )
  287.                   m_Access = "private ";
  288.                else if ( m_Getter.ThisMethodInfo.IsAssembly )
  289.                   m_Access = "internal ";
  290.                else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly )
  291.                   m_Access = "FamilyOrAssembly ";
  292.                else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly )
  293.                   m_Access = "FamilyAndAssembly ";
  294.             }
  295.             else{
  296.                if ( m_Setter.ThisMethodInfo.IsPublic )
  297.                   m_Access = "public ";
  298.                else if ( m_Setter.ThisMethodInfo.IsFamily )
  299.                   m_Access = "protected ";
  300.                else if ( m_Setter.ThisMethodInfo.IsPrivate )
  301.                   m_Access = "private ";
  302.                else if ( m_Setter.ThisMethodInfo.IsAssembly ) 
  303.                   m_Access = "internal ";
  304.                else if ( m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
  305.                   m_Access = "FamilyOrAssembly ";
  306.                else if ( m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
  307.                   m_Access = "FamilyAndAssembly ";
  308.             }
  309.             return m_Access;
  310.          }
  311.       }
  312.  
  313.       public String Attributes
  314.       {
  315.          override get { return this.Access; }
  316.       }      
  317.    }
  318.  
  319. } // namespace LangUtil
  320.  
  321.