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

  1. /*=====================================================================
  2.   File:      MCPropertyInfo.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.Text;
  25. using System.Reflection;
  26. using System.Collections;
  27.  
  28.    public class MCPropertyInfo : LangMember
  29.    {
  30.       internal String m_Name = String.Empty;
  31.       internal PropertyInfo m_ThisPropertyInfo = null;
  32.       internal MCLangType m_GetSetType = null;
  33.       internal MCMethodInfo m_Getter = null;
  34.       internal MCMethodInfo m_Setter = null;
  35.       internal ArrayList m_Parameters = new ArrayList();
  36.       internal String m_Access = String.Empty;
  37.  
  38.       public MCPropertyInfo( PropertyInfo myProperty )
  39.       {
  40.          try
  41.          {
  42.             if ( myProperty == null )
  43.                throw new NullReferenceException( "The PropertyInfo passed to the constructor is null." );
  44.  
  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.             {
  53.                m_Getter = new MCMethodInfo( myProperty.GetGetMethod( true ) );
  54.             }
  55.             if ( myProperty.CanWrite )
  56.             {
  57.                m_Setter = new MCMethodInfo( myProperty.GetSetMethod( true ) );
  58.             }
  59.  
  60.             MCMethodInfo obj;
  61.                
  62.             if ( myProperty.CanRead )
  63.             {
  64.                ParameterInfo[] tmpArray = myProperty.GetGetMethod( true ).GetParameters();
  65.  
  66.                // See Member.aspx, The last arg is return type of getter
  67.                for ( int i = 0; i < tmpArray.Length; i++ ) 
  68.                {
  69.                   m_Parameters.Add( new MCLangType( tmpArray[i] ) );
  70.                }     
  71.  
  72.                m_GetSetType = new MCLangType( myProperty.GetGetMethod( true ).ReturnType );
  73.                obj = m_Getter;
  74.             }
  75.             else if ( myProperty.CanWrite )
  76.             {
  77.                ParameterInfo[] tmpArray = myProperty.GetSetMethod( true ).GetParameters();
  78.  
  79.                // See Member.aspx, The last arg is not an arg of getter
  80.                for ( int i = 0; i < tmpArray.Length - 1; i++ )
  81.                {
  82.                   m_Parameters.Add( new MCLangType( tmpArray[i] ) );
  83.                }     
  84.  
  85.                // The last arg has same type as the type of this property
  86.                m_GetSetType = new MCLangType( tmpArray[tmpArray.Length - 1] );
  87.                obj = m_Setter;
  88.             }
  89.             else
  90.             {
  91.                throw new MissingMemberException( String.Format("{0}.{1} has neither a getter nor setter.",
  92.                                                   myProperty.ReflectedType.FullName,
  93.                                                   myProperty.Name ) );
  94.             }
  95.  
  96.          }
  97.          catch ( Exception Ex )
  98.          {
  99.             throw new Exception( Ex.GetType().Name + " error in property constructor.", Ex );
  100.          }
  101.       }
  102.  
  103.       public Type ThisType
  104.       {
  105.          override get { return m_ThisPropertyInfo.ReflectedType; }
  106.       }
  107.  
  108.       public String Name
  109.       {        
  110.          override get { return m_Name; }
  111.       }
  112.  
  113.       public MCLangType GetSetLangType
  114.       {
  115.          get { return m_GetSetType; }
  116.       }
  117.  
  118.  
  119.       public string PropertyAttributes(MethodInfo MI)
  120.  
  121.          {            
  122.             StringBuilder sb = new StringBuilder();
  123.             
  124.             if ( MI.IsPublic )
  125.                sb.Append( "public: " );
  126.             else if ( MI.IsPrivate )
  127.                sb.Append( "private: " );
  128.             else if ( MI.IsFamily )
  129.                sb.Append( "protected: " );
  130.             else if ( MI.IsAssembly )
  131.                sb.Append( "Assembly: " );
  132.             else if ( MI.IsFamilyAndAssembly )
  133.                sb.Append( "FamilyAndAssembly: " );
  134.             else if ( MI.IsFamilyOrAssembly )
  135.                sb.Append( "FamilyOrAssembly: " );
  136.             
  137.             if ( MI.IsFinal )
  138.                sb.Append( "__sealed " );
  139.             if ( MI.IsStatic )
  140.                sb.Append( "static " );
  141.             if ( MI.IsVirtual && !MI.DeclaringType.IsInterface )
  142.                sb.Append( "virtual " );
  143.  
  144.             sb.Append("__property ");
  145.             return sb.ToString();
  146.       }
  147.  
  148.  
  149.       public String TextDeclaration
  150.       {
  151.          override get
  152.          {
  153.             StringBuilder Syntax = new StringBuilder();
  154.             if ( m_ThisPropertyInfo.CanRead && (m_ThisPropertyInfo.GetGetMethod(true) != null))
  155.             {
  156.                StringBuilder temp = new StringBuilder(m_Getter.TextDeclaration);
  157.                temp.Replace(m_Getter.Attributes, PropertyAttributes(m_Getter.ThisMethodInfo));
  158.                Syntax.Append(temp.ToString());
  159.             }
  160.             if (m_ThisPropertyInfo.CanRead && m_ThisPropertyInfo.CanWrite)
  161.                 Syntax.Append("<BR>");
  162.             if ( m_ThisPropertyInfo.CanWrite && (m_ThisPropertyInfo.GetSetMethod(true) != null))
  163.             {
  164.                StringBuilder temp = new StringBuilder(m_Setter.TextDeclaration);
  165.                temp.Replace(m_Setter.Attributes, PropertyAttributes(m_Setter.ThisMethodInfo));
  166.                Syntax.Append(temp.ToString());
  167.             }
  168.             return Syntax.ToString();
  169.          }
  170.       }
  171.  
  172.       public string HTMLDeclaration
  173.       {
  174.          override get
  175.          {
  176.             StringBuilder Syntax = new StringBuilder();
  177.  
  178.             if ( m_ThisPropertyInfo.CanRead && (m_ThisPropertyInfo.GetGetMethod(true) != null))
  179.             {
  180.                StringBuilder temp = new StringBuilder(m_Getter.HTMLDeclaration);
  181.                temp.Replace(m_Getter.Attributes, PropertyAttributes(m_Getter.ThisMethodInfo));
  182.                Syntax.Append(temp.ToString());
  183.             }
  184.             if ( m_ThisPropertyInfo.CanWrite && (m_ThisPropertyInfo.GetSetMethod(true) != null))
  185.             {
  186.                StringBuilder temp = new StringBuilder(m_Setter.HTMLDeclaration);
  187.                temp.Replace(m_Setter.Attributes, PropertyAttributes(m_Setter.ThisMethodInfo));
  188.                Syntax.Append(temp.ToString());
  189.             }
  190.  
  191.            return Syntax.ToString();
  192.          }
  193.       }
  194.  
  195.       // Access returns a string containing the level of access exposed by the accessor methods for this property 
  196.       // IFF the two levels are the same. If they are not, it throws an exception. 
  197.  
  198.       protected String Access
  199.       {
  200.          get
  201.          {
  202.             String m_Access = String.Empty;
  203.                
  204.             if ( m_ThisPropertyInfo.CanRead && m_ThisPropertyInfo.CanWrite )
  205.             {
  206.                if ( m_Getter.ThisMethodInfo.IsPublic && m_Setter.ThisMethodInfo.IsPublic )
  207.                   m_Access = "public: ";
  208.                else if ( m_Getter.ThisMethodInfo.IsFamily && m_Setter.ThisMethodInfo.IsFamily )
  209.                   m_Access = "protected: ";
  210.                else if ( m_Getter.ThisMethodInfo.IsPrivate && m_Setter.ThisMethodInfo.IsPrivate )
  211.                   m_Access = "private: ";
  212.                else if ( m_Getter.ThisMethodInfo.IsAssembly && m_Setter.ThisMethodInfo.IsAssembly )
  213.                   m_Access = "Assembly: ";
  214.                else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly && m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
  215.                   m_Access = "FamilyOrAssembly: ";
  216.                else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly && m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
  217.                   m_Access = "FamilyAndAssembly: ";
  218.                else
  219.                {
  220.                   throw new Exception( "Accessor-methods claim to agree but metadata disagrees with this. On property: "
  221.                            + m_ThisPropertyInfo.ReflectedType.FullName + "."
  222.                            + m_ThisPropertyInfo.Name );
  223.                }
  224.             }
  225.             else if ( m_ThisPropertyInfo.CanRead )
  226.             {
  227.                if ( m_Getter.ThisMethodInfo.IsPublic )
  228.                   m_Access = "public: ";
  229.                else if ( m_Getter.ThisMethodInfo.IsFamily )
  230.                   m_Access = "protected: ";
  231.                else if ( m_Getter.ThisMethodInfo.IsPrivate )
  232.                   m_Access = "private: ";
  233.                else if ( m_Getter.ThisMethodInfo.IsAssembly )
  234.                   m_Access = "Assembly: ";
  235.                else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly )
  236.                   m_Access = "FamilyOrAssembly: ";
  237.                else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly )
  238.                   m_Access = "FamilyAndAssembly: ";
  239.             }
  240.             else
  241.             {
  242.                if ( m_Setter.ThisMethodInfo.IsPublic )
  243.                   m_Access = "public: ";
  244.                else if ( m_Setter.ThisMethodInfo.IsFamily )
  245.                   m_Access = "protected: ";
  246.                else if ( m_Setter.ThisMethodInfo.IsPrivate )
  247.                   m_Access = "private: ";
  248.                else if ( m_Setter.ThisMethodInfo.IsAssembly )
  249.                   m_Access = "Assembly: ";
  250.                else if ( m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
  251.                   m_Access = "FamilyOrAssembly: ";
  252.                else if ( m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
  253.                   m_Access = "FamilyAndAssembly: ";
  254.             }
  255.             m_Access += "__property ";          
  256.             return m_Access;
  257.          }
  258.       }
  259.  
  260.       public string Attributes
  261.       {
  262.          override get { return this.Access; }
  263.       }
  264.       
  265.       public override bool IsProperty()
  266.       {
  267.          return true;
  268.       }
  269.    }
  270.  
  271. } // namespace LangUtil
  272.