home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: NativePropertyInfo.cs
-
- Summary: For language-neutral reflection information.
-
- ---------------------------------------------------------------------
- This file is part of the Microsoft NGWS SDK Code Samples.
-
- Copyright (C) 2000 Microsoft Corporation. All rights reserved.
-
- This source code is intended only as a supplement to Microsoft
- Development Tools and/or on-line documentation. See these other
- materials for detailed information regarding Microsoft code samples.
-
- THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
- KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
- PARTICULAR PURPOSE.
- =====================================================================*/
-
- namespace LangUtil {
-
- using System;
- using System.Reflection;
- using System.Collections;
-
- public class NativePropertyInfo : LangMember
- {
- internal String m_Name = String.Empty;
- internal PropertyInfo m_ThisPropertyInfo = null;
- internal NativeLangType m_GetSetType = null;
- internal NativeMethodInfo m_Getter = null;
- internal NativeMethodInfo m_Setter = null;
- internal ArrayList m_Parameters = new ArrayList();
- internal String m_Access = String.Empty;
-
- public NativePropertyInfo( PropertyInfo myProperty )
- {
- try
- {
- if ( myProperty == null )
- throw new NullReferenceException( "The PropertyInfo passed to the constructor is null." );
-
- // Obtain Property Name and Type
- m_Name = myProperty.Name;
- m_ThisPropertyInfo = myProperty;
-
- // Obtain Getter and Setter
- if ( myProperty.CanRead )
- {
- if ( myProperty.GetGetMethod( true ) != null )
- m_Getter = new NativeMethodInfo( myProperty.GetGetMethod( true ) );
- else
- throw new NullReferenceException( "Property says it's CanRead but GetGetMethod(true)"
- + " returns null for property:\""
- + myProperty.ReflectedType.Name
- + "."
- + myProperty.Name
- + "\"." );
- }
-
- if ( myProperty.CanWrite )
- {
- if ( myProperty.GetSetMethod( true ) != null )
- m_Setter = new NativeMethodInfo( myProperty.GetSetMethod( true ) );
- else
- throw new NullReferenceException( "Property says it's CanWrite but GetSetMethod(true)"
- + " returns null for property:\""
- + myProperty.ReflectedType.Name
- + "."
- + myProperty.Name
- + "\".");
- }
-
-
- NativeMethodInfo obj;
-
- if ( myProperty.CanRead )
- {
- ParameterInfo[] tmpArray = myProperty.GetGetMethod( true ).GetParameters();
-
- // See Member.aspx, The last arg is return type of getter
- for ( int i = 0; i < tmpArray.Length; i++ )
- {
- m_Parameters.Add( new NativeLangType( tmpArray[i] ) );
- }
-
- m_GetSetType = new NativeLangType( myProperty.GetGetMethod( true ).ReturnType );
- obj = m_Getter;
- }
- else if ( myProperty.CanWrite )
- {
- ParameterInfo[] tmpArray = myProperty.GetSetMethod( true ).GetParameters();
-
- // See Member.aspx, The last arg is not an arg of getter
- for ( int i = 0; i < tmpArray.Length - 1; i++ )
- {
- m_Parameters.Add( new NativeLangType( tmpArray[i] ) );
- }
-
- // The last arg has same type as the type of this property
- m_GetSetType = new NativeLangType( tmpArray[tmpArray.Length - 1] );
- obj = m_Setter;
- }
- else
- {
- throw new MissingMemberException( String.Format( "{0}.{1} has neither a getter nor setter.",
- myProperty.ReflectedType.FullName,
- myProperty.Name ) );
- }
-
- }
- catch ( Exception Ex )
- {
- throw new Exception( Ex.GetType().Name + " error in NativePropertyInfo constructor.", Ex );
- }
- }
-
- public CSharpPropertyInfo CSharpLangObject
- {
- get { return new CSharpPropertyInfo( m_ThisPropertyInfo ); }
- }
-
- public MCPropertyInfo MCLangObject
- {
- get { return new MCPropertyInfo( m_ThisPropertyInfo ); }
- }
-
- public PropertyInfo ThisPropertyInfo
- {
- get { return m_ThisPropertyInfo; }
- }
-
- public VisualBasicPropertyInfo VisualBasicLangObject
- {
- get { return new VisualBasicPropertyInfo( m_ThisPropertyInfo ); }
- }
-
- public NativeLangType GetSetLangType
- {
- get { return m_GetSetType; }
- }
-
- public Type ThisType
- {
- override get { return m_ThisPropertyInfo.ReflectedType; }
- }
-
- public String Name
- {
- override get { return m_Name; }
- }
-
-
- // Access returns a string containing the level of access exposed by the accessor methods for this property
- // IFF the two levels are the same. If they are not, it throws an exception.
-
- protected String Access
- {
- get
- {
- String m_Access = String.Empty;
-
- if ( m_ThisPropertyInfo.CanRead && m_ThisPropertyInfo.CanWrite )
- {
- if ( m_Getter.ThisMethodInfo.IsPublic && m_Setter.ThisMethodInfo.IsPublic )
- m_Access = "public ";
- else if ( m_Getter.ThisMethodInfo.IsFamily && m_Setter.ThisMethodInfo.IsFamily )
- m_Access = "protected ";
- else if ( m_Getter.ThisMethodInfo.IsPrivate && m_Setter.ThisMethodInfo.IsPrivate )
- m_Access = "private ";
- else if ( m_Getter.ThisMethodInfo.IsAssembly && m_Setter.ThisMethodInfo.IsAssembly )
- m_Access = "internal ";
- else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly && m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
- m_Access = "FamilyOrAssembly ";
- else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly && m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
- m_Access = "FamilyAndAssembly ";
- else
- {
- throw new Exception( "Accessor-methods claim to agree but metadata disagrees with this. On property: "
- + m_ThisPropertyInfo.ReflectedType.FullName + "."
- + m_ThisPropertyInfo.Name );
- }
- }
- else if ( m_ThisPropertyInfo.CanRead )
- {
- if ( m_Getter.ThisMethodInfo.IsPublic )
- m_Access = "public ";
- else if ( m_Getter.ThisMethodInfo.IsFamily )
- m_Access = "protected ";
- else if ( m_Getter.ThisMethodInfo.IsPrivate )
- m_Access = "private ";
- else if ( m_Getter.ThisMethodInfo.IsAssembly )
- m_Access = "internal ";
- else if ( m_Getter.ThisMethodInfo.IsFamilyOrAssembly )
- m_Access = "FamilyOrAssembly ";
- else if ( m_Getter.ThisMethodInfo.IsFamilyAndAssembly )
- m_Access = "FamilyAndAssembly ";
- }
- else
- {
- if ( m_Setter.ThisMethodInfo.IsPublic )
- m_Access = "public ";
- else if ( m_Setter.ThisMethodInfo.IsFamily )
- m_Access = "protected ";
- else if ( m_Setter.ThisMethodInfo.IsPrivate )
- m_Access = "private ";
- else if ( m_Setter.ThisMethodInfo.IsAssembly )
- m_Access = "internal ";
- else if ( m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
- m_Access = "FamilyOrAssembly ";
- else if ( m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
- m_Access = "FamilyAndAssembly ";
- }
- return m_Access;
- }
- }
-
- public String Attributes
- {
- override get { return this.Access; }
- }
-
- public override bool IsProperty()
- {
- return true;
- }
- }
-
- } // namespace LangUtil
-