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

  1. /*=====================================================================
  2.   File:      VisualBasicPropertyDetails.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.IO;
  25. using System.Text;
  26. using System.Reflection;
  27. using System.Collections;
  28.  
  29.    public class VisualBasicPropertyInfo : LangMember
  30.    {
  31.       internal string m_Name = String.Empty;
  32.       internal PropertyInfo m_ThisPropertyInfo = null;
  33.       internal VisualBasicLangType m_GetSetType = null;
  34.       internal VisualBasicMethodInfo m_Getter = null;
  35.       internal VisualBasicMethodInfo m_Setter = null;
  36.       internal ArrayList m_Parameters = new ArrayList();
  37.       internal String m_Access = String.Empty;
  38.  
  39.       public VisualBasicPropertyInfo( PropertyInfo myProperty )
  40.       {
  41.          try
  42.          {
  43.             if ( myProperty == null )
  44.                throw new NullReferenceException( "The PropertyInfo passed to the constructor is null." );
  45.  
  46.             // Obtain Property Name and Type
  47.             m_Name = myProperty.Name;
  48.             m_ThisPropertyInfo = myProperty;
  49.  
  50.             // Obtain Getter and Setter
  51.             if ( myProperty.CanRead )
  52.                m_Getter = new VisualBasicMethodInfo(myProperty.GetGetMethod( true ) );
  53.             if ( myProperty.CanWrite )
  54.                m_Setter = new VisualBasicMethodInfo( myProperty.GetSetMethod( true ) );
  55.  
  56.             VisualBasicMethodInfo obj;
  57.             if ( myProperty.CanRead )
  58.             {
  59.                ParameterInfo[] tmpArray = myProperty.GetGetMethod( true ).GetParameters();
  60.  
  61.                // See Member.aspx, The last arg is return type of getter
  62.                for ( int i = 0; i < tmpArray.Length; i++ )
  63.                   m_Parameters.Add( new VisualBasicLangType( tmpArray[i] ) );
  64.  
  65.                m_GetSetType = new VisualBasicLangType( myProperty.GetGetMethod( true ).ReturnType );
  66.                obj = m_Getter;
  67.             }
  68.             else if ( myProperty.CanWrite )
  69.             {
  70.                ParameterInfo[] tmpArray = myProperty.GetSetMethod( true ).GetParameters();
  71.  
  72.                // See Member.aspx, The last arg is not an arg of getter
  73.                for ( int i = 0; i < tmpArray.Length - 1; i++ )
  74.                   m_Parameters.Add( new VisualBasicLangType( tmpArray[i] ) );
  75.  
  76.                // The last arg has same type as the type of this property
  77.                m_GetSetType = new VisualBasicLangType( tmpArray[tmpArray.Length - 1] );
  78.                obj = m_Setter;
  79.             }
  80.             else
  81.             {
  82.                throw new MissingMemberException( String.Format( "{0}.{1} has neither a getter nor setter.",
  83.                                                   myProperty.ReflectedType.FullName,
  84.                                                   myProperty.Name ) );
  85.             }
  86.          }
  87.          catch ( Exception Ex )
  88.          {
  89.             throw new Exception( Ex.GetType().Name + " error in property constructor at: " + Ex.TargetSite.Name, Ex );
  90.          }
  91.       }
  92.  
  93.       public Type ThisType
  94.       {
  95.          override get { return m_ThisPropertyInfo.ReflectedType; }
  96.       }
  97.  
  98.       public String Name 
  99.       {
  100.          override get { return m_Name; }
  101.       }
  102.  
  103.       public VisualBasicLangType GetSetLangType
  104.       {
  105.          get { return m_GetSetType; }
  106.       }
  107.  
  108.  
  109.       public String TextDeclaration
  110.       {
  111.          override get
  112.          {            
  113.             StringBuilder Syntax = new StringBuilder( String.Empty );
  114.  
  115.             if ( m_ThisPropertyInfo.CanRead ) 
  116.             {
  117.                Syntax.Append( Attributes + "Get " + m_Name + "(" );
  118.  
  119.                for ( int j = 0; j < m_Parameters.Count; j++ ) 
  120.                { 
  121.                   if ( ( (VisualBasicLangType)m_Parameters[j] ).IsByRef )
  122.                      Syntax.Append( "ByRef " );
  123.                   else
  124.                      Syntax.Append( "ByVal " );
  125.  
  126.                   Syntax.Append( ( (VisualBasicLangType)m_Parameters[j] ).ParamVarName );
  127.  
  128.                   if ( ( (VisualBasicLangType)m_Parameters[j] ).m_IsArray )
  129.                      Syntax.Append( "()" );
  130.  
  131.                   Syntax.Append( " As " + ( (VisualBasicLangType)m_Parameters[j] ).ClassName );
  132.  
  133.                   if ( j != ( m_Parameters.Count - 1 ) ) 
  134.                      Syntax.Append( ", " );
  135.                } 
  136.  
  137.                Syntax.Append( ") As " );
  138.  
  139.                if ( m_GetSetType.m_IsByRef )
  140.                   Syntax.Append( "ByRef " );
  141.  
  142.                Syntax.Append( m_GetSetType.ClassName );
  143.  
  144.                if ( m_GetSetType.m_IsArray )
  145.                   Syntax.Append( "()" );
  146.             }
  147.             
  148.             if ( m_ThisPropertyInfo.CanWrite )
  149.             {
  150.                Syntax.Append( " " + Attributes );
  151.  
  152.                Type temptype = m_ThisPropertyInfo.PropertyType;
  153.                if ( temptype.IsValueType || ( temptype == typeof( string ) ) )
  154.                   Syntax.Append( "Let " );
  155.                else if ( ( temptype.IsArray && ( temptype != typeof( Array ) ) ) || ( !temptype.IsValueType && temptype != typeof( string ) ) )
  156.                   Syntax.Append( "Set " );
  157.                else
  158.                   Syntax.Append( "Let " );
  159.                
  160.                Syntax.Append( m_Name );
  161.                Syntax.Append( "(" );
  162.  
  163.                for ( int j = 0; j < m_Parameters.Count; j++ )
  164.                { 
  165.                   if ( ( (VisualBasicLangType)m_Parameters[j] ).IsByRef )
  166.                      Syntax.Append( "ByRef " );
  167.                   else
  168.                      Syntax.Append( "ByVal " );
  169.  
  170.                   Syntax.Append( "" + ( (VisualBasicLangType)m_Parameters[j] ).ParamVarName );
  171.  
  172.                   if ( ( (VisualBasicLangType)m_Parameters[j] ).m_IsArray )
  173.                      Syntax.Append( "()" );
  174.  
  175.                   Syntax.Append( " As " + ( (VisualBasicLangType)m_Parameters[j] ).ClassName );
  176.                   Syntax.Append( ", " );                  
  177.                }
  178.  
  179.                Syntax.Append( "ByVal value" );
  180.                if ( temptype.IsArray )
  181.                   Syntax.Append( "()" );
  182.                Syntax.Append( " As " );
  183.  
  184.                // Not yet implemented: ClassName will not be the name of the class if it's HasElementType.
  185.                Syntax.Append( m_GetSetType.ClassName );
  186.                Syntax.Append( ")" );
  187.             }
  188.             return Syntax.ToString();
  189.          }
  190.       }
  191.  
  192.       public String HTMLDeclaration
  193.       {
  194.          override get
  195.          {            
  196.             StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
  197.             Syntax.Append( "<span style=\"color:red\">[Visual Basic]</span><BR><B>" );
  198.             if ( m_ThisPropertyInfo.CanRead ) 
  199.             {
  200.                Syntax.Append( Attributes );
  201.                Syntax.Append( "Get " );
  202.                Syntax.Append( m_Name );
  203.                Syntax.Append( "(" );
  204.                for ( int j = 0; j < m_Parameters.Count; j++ ) 
  205.                { 
  206.                   if ( ( (VisualBasicLangType)m_Parameters[j]).IsByRef )
  207.                      Syntax.Append( "ByRef <I>" );
  208.                   else
  209.                      Syntax.Append( "ByVal <I>" );
  210.                   Syntax.Append( ( (VisualBasicLangType)m_Parameters[j] ).ParamVarName + "</I>" );
  211.                   if ( ( (VisualBasicLangType)m_Parameters[j] ).m_IsArray )
  212.                      Syntax.Append( "()" );
  213.                   Syntax.Append( " As " + ( (VisualBasicLangType)m_Parameters[j] ).ClassName );
  214.                   if ( j != ( m_Parameters.Count - 1 ) )
  215.                   { 
  216.                      Syntax.Append( ", " );
  217.                   } 
  218.                } 
  219.                Syntax.Append( ")" );
  220.                Syntax.Append( " As " );
  221.                if ( m_GetSetType.m_IsByRef )
  222.                   Syntax.Append( "ByRef " );
  223.                Syntax.Append( m_GetSetType.ClassName );
  224.                if ( m_GetSetType.m_IsArray )
  225.                   Syntax.Append( "()" );
  226.             }
  227.  
  228.             if ( m_ThisPropertyInfo.CanWrite )
  229.             {
  230.                if ( m_ThisPropertyInfo.CanRead )
  231.                   Syntax.Append( "<BR>" );
  232.                Syntax.Append( Attributes );
  233.                Type temptype = m_GetSetType.ThisType;
  234.  
  235.                if ( temptype.IsValueType || temptype == typeof( string ) )
  236.                   Syntax.Append( "Let " );
  237.                else if ( ( temptype.IsArray && ( temptype != typeof( Array ) ) ) || 
  238.                          (!temptype.IsValueType && temptype!= typeof( System.String ) ) )
  239.                   Syntax.Append( "Set " );
  240.                else 
  241.                   Syntax.Append( "Let " );
  242.  
  243.                temptype = null;
  244.                Syntax.Append( m_Name );
  245.                Syntax.Append( "(" );
  246.                for ( int j = 0; j < m_Parameters.Count; j++ ) 
  247.                { 
  248.                   if ( ( (VisualBasicLangType)m_Parameters[j] ).IsByRef )
  249.                      Syntax.Append( "ByRef " );
  250.                   else
  251.                      Syntax.Append( "ByVal " );
  252.                   Syntax.Append( "<I>" + ( (VisualBasicLangType)m_Parameters[j] ).ParamVarName + "</I>" );
  253.                   if ( ( (VisualBasicLangType)m_Parameters[j] ).m_IsArray )
  254.                      Syntax.Append( "()" );
  255.                   Syntax.Append( " As " + ( (VisualBasicLangType)m_Parameters[j] ).ClassName );
  256.                      Syntax.Append( ", " );
  257.                }
  258.                Syntax.Append( "ByVal <I>value</I> As " );
  259.                Syntax.Append( m_GetSetType.ClassName );
  260.                Syntax.Append( ")" );
  261.             }
  262.             Syntax.Append( "</B></PRE>" );
  263.             return Syntax.ToString();
  264.          }
  265.       }
  266.  
  267.       // Access returns a string containing the level of access exposed by the accessor methods for this property 
  268.       // IFF the two levels are the same. If they are not, it throws and exception. 
  269.  
  270.       protected String Access
  271.       {
  272.          get
  273.          {
  274.             string m_Access = String.Empty;
  275.  
  276.             // Determine if this is a default property            
  277.             MemberInfo[] mi = ThisType.GetDefaultMembers();
  278.             if ( mi != null )
  279.             {
  280.                for ( int i = 0; i < mi.Length; i++ )
  281.                {
  282.                   if ( mi[i] == m_ThisPropertyInfo )
  283.                   {
  284.                      m_Access += "Default ";
  285.                      break;
  286.                   }
  287.                }
  288.             }
  289.  
  290.             if ( m_ThisPropertyInfo.CanRead && m_ThisPropertyInfo.CanWrite )
  291.             {
  292.                if ( m_Getter.ThisMethodInfo.IsPublic && m_Setter.ThisMethodInfo.IsPublic )
  293.                   m_Access += "Public ";
  294.                else if ( m_Getter.ThisMethodInfo.IsFamily && m_Setter.ThisMethodInfo.IsFamily )
  295.                   m_Access += "Protected ";
  296.                else if ( m_Getter.ThisMethodInfo.IsPrivate && m_Setter.ThisMethodInfo.IsPrivate )
  297.                   m_Access += "Private ";
  298.                else if ( m_Getter.ThisMethodInfo.IsAssembly && m_Setter.ThisMethodInfo.IsAssembly )
  299.                   m_Access += "Assembly ";
  300.                else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly && m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
  301.                   m_Access += "FamilyOrAssembly ";
  302.                else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly && m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
  303.                   m_Access += "FamilyAndAssembly ";
  304.                else
  305.                   throw new Exception( "Accessor-methods do not have the same level of access on the "
  306.                       + m_ThisPropertyInfo.ReflectedType.FullName + "."
  307.                       + m_ThisPropertyInfo.Name
  308.                       + " property." );
  309.             }
  310.             else if ( m_ThisPropertyInfo.CanRead )
  311.             {
  312.                if ( m_Getter.ThisMethodInfo.IsPublic )
  313.                   m_Access += "Public ";
  314.                else if ( m_Getter.ThisMethodInfo.IsFamily )
  315.                   m_Access += "Protected ";
  316.                else if ( m_Getter.ThisMethodInfo.IsPrivate )
  317.                   m_Access += "Private ";
  318.                else if ( m_Getter.ThisMethodInfo.IsAssembly )
  319.                   m_Access += "Assembly ";
  320.                else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly )
  321.                   m_Access += "FamilyOrAssembly ";
  322.                else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly )
  323.                   m_Access += "FamilyAndAssembly ";
  324.             }
  325.             else
  326.             {
  327.                if ( m_Setter.ThisMethodInfo.IsPublic )
  328.                   m_Access += "Public ";
  329.                else if ( m_Setter.ThisMethodInfo.IsFamily )
  330.                   m_Access += "Protected ";
  331.                else if ( m_Setter.ThisMethodInfo.IsPrivate )
  332.                   m_Access += "Private ";
  333.                else if ( m_Setter.ThisMethodInfo.IsAssembly )
  334.                   m_Access += "Assembly ";
  335.                else if ( m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
  336.                   m_Access += "FamilyOrAssembly ";
  337.                else if ( m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
  338.                   m_Access += "FamilyAndAssembly ";
  339.             }
  340.             return m_Access;
  341.          }
  342.       }
  343.  
  344.       public string Attributes
  345.       {
  346.          override get
  347.          {
  348.             StringBuilder sb = new StringBuilder( this.Access );
  349.             sb.Append( "Property " );
  350.             return sb.ToString();
  351.          }
  352.       }
  353.       
  354.       public override bool IsProperty()
  355.       {
  356.          return true;
  357.       }
  358.    }
  359.  
  360. } // namespace LangUtil
  361.