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

  1. /*=====================================================================
  2.   File:      NativePropertyInfo.cs
  3.  
  4.   Summary:   For language-neutral reflection information.
  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.Reflection;
  25. using System.Collections;
  26.  
  27.    public class NativePropertyInfo : LangMember
  28.    {
  29.       internal String m_Name = String.Empty;
  30.       internal PropertyInfo m_ThisPropertyInfo = null;
  31.       internal NativeLangType m_GetSetType = null;
  32.       internal NativeMethodInfo m_Getter = null;
  33.       internal NativeMethodInfo m_Setter = null;
  34.       internal ArrayList m_Parameters = new ArrayList();
  35.       internal String m_Access = String.Empty;
  36.  
  37.       public NativePropertyInfo( PropertyInfo myProperty )
  38.       {
  39.          try
  40.          {
  41.             if ( myProperty == null )
  42.                throw new NullReferenceException( "The PropertyInfo passed to the constructor is null." );
  43.  
  44.             // Obtain Property Name and Type
  45.             m_Name = myProperty.Name;
  46.             m_ThisPropertyInfo = myProperty;
  47.  
  48.             // Obtain Getter and Setter
  49.             if ( myProperty.CanRead )
  50.             {
  51.                if ( myProperty.GetGetMethod( true ) != null )
  52.                   m_Getter = new NativeMethodInfo( myProperty.GetGetMethod( true ) );
  53.                else
  54.                   throw new NullReferenceException( "Property says it's CanRead but GetGetMethod(true)"
  55.                      + " returns null for property:\""
  56.                      + myProperty.ReflectedType.Name
  57.                      + "."
  58.                      + myProperty.Name
  59.                      + "\"." );
  60.             }
  61.  
  62.             if ( myProperty.CanWrite )
  63.             {
  64.                if ( myProperty.GetSetMethod( true ) != null )
  65.                   m_Setter = new NativeMethodInfo( myProperty.GetSetMethod( true ) );
  66.                else
  67.                   throw new NullReferenceException( "Property says it's CanWrite but GetSetMethod(true)"
  68.                      + " returns null for property:\""
  69.                      + myProperty.ReflectedType.Name
  70.                      + "."
  71.                      + myProperty.Name
  72.                      + "\".");
  73.             }
  74.  
  75.  
  76.             NativeMethodInfo obj;
  77.                
  78.             if ( myProperty.CanRead )
  79.             {
  80.                ParameterInfo[] tmpArray = myProperty.GetGetMethod( true ).GetParameters();
  81.  
  82.                // See Member.aspx, The last arg is return type of getter
  83.                for ( int i = 0; i < tmpArray.Length; i++ )
  84.                {
  85.                   m_Parameters.Add( new NativeLangType( tmpArray[i] ) );
  86.                }     
  87.  
  88.                m_GetSetType = new NativeLangType( myProperty.GetGetMethod( true ).ReturnType );
  89.                obj = m_Getter;
  90.             }
  91.             else if ( myProperty.CanWrite )
  92.             {
  93.                ParameterInfo[] tmpArray = myProperty.GetSetMethod( true ).GetParameters();
  94.  
  95.                // See Member.aspx, The last arg is not an arg of getter
  96.                for ( int i = 0; i < tmpArray.Length - 1; i++ )
  97.                {
  98.                   m_Parameters.Add( new NativeLangType( tmpArray[i] ) );
  99.                }     
  100.  
  101.                // The last arg has same type as the type of this property
  102.                m_GetSetType = new NativeLangType( tmpArray[tmpArray.Length - 1] );
  103.                obj = m_Setter;
  104.             }
  105.             else
  106.             {                            
  107.                throw new MissingMemberException( String.Format( "{0}.{1} has neither a getter nor setter.",
  108.                                                    myProperty.ReflectedType.FullName,
  109.                                                    myProperty.Name ) );
  110.             }
  111.  
  112.          }
  113.          catch ( Exception Ex )
  114.          {
  115.             throw new Exception( Ex.GetType().Name + " error in NativePropertyInfo constructor.", Ex );
  116.          }
  117.       }
  118.  
  119.       public CSharpPropertyInfo CSharpLangObject
  120.       {
  121.          get { return new CSharpPropertyInfo( m_ThisPropertyInfo ); }
  122.       }
  123.       
  124.       public MCPropertyInfo MCLangObject
  125.       {
  126.          get { return new MCPropertyInfo( m_ThisPropertyInfo ); }
  127.       }
  128.  
  129.       public PropertyInfo ThisPropertyInfo
  130.       {
  131.          get { return m_ThisPropertyInfo; }
  132.       }
  133.  
  134.       public VisualBasicPropertyInfo VisualBasicLangObject
  135.       {
  136.          get { return new VisualBasicPropertyInfo( m_ThisPropertyInfo ); }
  137.       }
  138.  
  139.       public NativeLangType GetSetLangType
  140.       {
  141.          get { return m_GetSetType; }
  142.       }
  143.  
  144.       public Type ThisType
  145.       {
  146.          override get { return m_ThisPropertyInfo.ReflectedType; }
  147.       }
  148.  
  149.       public String Name
  150.       {
  151.          override get { return m_Name; }
  152.       }
  153.  
  154.  
  155.       // Access returns a string containing the level of access exposed by the accessor methods for this property 
  156.       // IFF the two levels are the same. If they are not, it throws an exception. 
  157.  
  158.       protected String Access
  159.       {
  160.          get
  161.          {
  162.             String m_Access = String.Empty;
  163.          
  164.             if ( m_ThisPropertyInfo.CanRead && m_ThisPropertyInfo.CanWrite )
  165.             {
  166.                if ( m_Getter.ThisMethodInfo.IsPublic && m_Setter.ThisMethodInfo.IsPublic )
  167.                   m_Access = "public ";
  168.                else if ( m_Getter.ThisMethodInfo.IsFamily && m_Setter.ThisMethodInfo.IsFamily )
  169.                   m_Access = "protected ";
  170.                else if ( m_Getter.ThisMethodInfo.IsPrivate && m_Setter.ThisMethodInfo.IsPrivate )
  171.                   m_Access = "private ";
  172.                else if ( m_Getter.ThisMethodInfo.IsAssembly && m_Setter.ThisMethodInfo.IsAssembly )
  173.                   m_Access = "internal ";
  174.                else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly && m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
  175.                   m_Access = "FamilyOrAssembly ";
  176.                else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly && m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
  177.                   m_Access = "FamilyAndAssembly ";
  178.                else
  179.                {
  180.                   throw new Exception( "Accessor-methods claim to agree but metadata disagrees with this. On property: "
  181.                      + m_ThisPropertyInfo.ReflectedType.FullName + "."
  182.                      + m_ThisPropertyInfo.Name );
  183.                }
  184.             }
  185.             else if ( m_ThisPropertyInfo.CanRead )
  186.             {
  187.                if ( m_Getter.ThisMethodInfo.IsPublic )
  188.                   m_Access = "public ";
  189.                else if ( m_Getter.ThisMethodInfo.IsFamily )
  190.                   m_Access = "protected ";
  191.                else if ( m_Getter.ThisMethodInfo.IsPrivate )
  192.                   m_Access = "private ";
  193.                else if ( m_Getter.ThisMethodInfo.IsAssembly )
  194.                   m_Access = "internal ";
  195.                else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly )
  196.                   m_Access = "FamilyOrAssembly ";
  197.                else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly )
  198.                   m_Access = "FamilyAndAssembly ";
  199.             }
  200.             else
  201.             {
  202.                if ( m_Setter.ThisMethodInfo.IsPublic )
  203.                   m_Access = "public ";
  204.                else if ( m_Setter.ThisMethodInfo.IsFamily )
  205.                   m_Access = "protected ";
  206.                else if ( m_Setter.ThisMethodInfo.IsPrivate )
  207.                   m_Access = "private ";
  208.                else if ( m_Setter.ThisMethodInfo.IsAssembly )
  209.                   m_Access = "internal ";
  210.                else if ( m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
  211.                   m_Access = "FamilyOrAssembly ";
  212.                else if ( m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
  213.                   m_Access = "FamilyAndAssembly ";
  214.             }
  215.             return m_Access;
  216.          }
  217.       }
  218.  
  219.       public String Attributes
  220.       {
  221.          override get { return this.Access; }
  222.       }
  223.       
  224.       public override bool IsProperty()
  225.       {
  226.          return true;
  227.       }
  228.    }
  229.  
  230. } // namespace LangUtil
  231.