home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: MCPropertyInfo.cs
-
- Summary: Brief summary of the file contents and purpose.
-
- ---------------------------------------------------------------------
- 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.Text;
- using System.Reflection;
- using System.Collections;
-
- public class MCPropertyInfo : LangMember
- {
- internal String m_Name = String.Empty;
- internal PropertyInfo m_ThisPropertyInfo = null;
- internal MCLangType m_GetSetType = null;
- internal MCMethodInfo m_Getter = null;
- internal MCMethodInfo m_Setter = null;
- internal ArrayList m_Parameters = new ArrayList();
- internal String m_Access = String.Empty;
-
- public MCPropertyInfo( 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 )
- {
- m_Getter = new MCMethodInfo( myProperty.GetGetMethod( true ) );
- }
- if ( myProperty.CanWrite )
- {
- m_Setter = new MCMethodInfo( myProperty.GetSetMethod( true ) );
- }
-
- MCMethodInfo 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 MCLangType( tmpArray[i] ) );
- }
-
- m_GetSetType = new MCLangType( 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 MCLangType( tmpArray[i] ) );
- }
-
- // The last arg has same type as the type of this property
- m_GetSetType = new MCLangType( 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 property constructor.", Ex );
- }
- }
-
- public Type ThisType
- {
- override get { return m_ThisPropertyInfo.ReflectedType; }
- }
-
- public String Name
- {
- override get { return m_Name; }
- }
-
- public MCLangType GetSetLangType
- {
- get { return m_GetSetType; }
- }
-
-
- public string PropertyAttributes(MethodInfo MI)
-
- {
- StringBuilder sb = new StringBuilder();
-
- if ( MI.IsPublic )
- sb.Append( "public: " );
- else if ( MI.IsPrivate )
- sb.Append( "private: " );
- else if ( MI.IsFamily )
- sb.Append( "protected: " );
- else if ( MI.IsAssembly )
- sb.Append( "Assembly: " );
- else if ( MI.IsFamilyAndAssembly )
- sb.Append( "FamilyAndAssembly: " );
- else if ( MI.IsFamilyOrAssembly )
- sb.Append( "FamilyOrAssembly: " );
-
- if ( MI.IsFinal )
- sb.Append( "__sealed " );
- if ( MI.IsStatic )
- sb.Append( "static " );
- if ( MI.IsVirtual && !MI.DeclaringType.IsInterface )
- sb.Append( "virtual " );
-
- sb.Append("__property ");
- return sb.ToString();
- }
-
-
- public String TextDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder();
- if ( m_ThisPropertyInfo.CanRead && (m_ThisPropertyInfo.GetGetMethod(true) != null))
- {
- StringBuilder temp = new StringBuilder(m_Getter.TextDeclaration);
- temp.Replace(m_Getter.Attributes, PropertyAttributes(m_Getter.ThisMethodInfo));
- Syntax.Append(temp.ToString());
- }
- if (m_ThisPropertyInfo.CanRead && m_ThisPropertyInfo.CanWrite)
- Syntax.Append("<BR>");
- if ( m_ThisPropertyInfo.CanWrite && (m_ThisPropertyInfo.GetSetMethod(true) != null))
- {
- StringBuilder temp = new StringBuilder(m_Setter.TextDeclaration);
- temp.Replace(m_Setter.Attributes, PropertyAttributes(m_Setter.ThisMethodInfo));
- Syntax.Append(temp.ToString());
- }
- return Syntax.ToString();
- }
- }
-
- public string HTMLDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder();
-
- if ( m_ThisPropertyInfo.CanRead && (m_ThisPropertyInfo.GetGetMethod(true) != null))
- {
- StringBuilder temp = new StringBuilder(m_Getter.HTMLDeclaration);
- temp.Replace(m_Getter.Attributes, PropertyAttributes(m_Getter.ThisMethodInfo));
- Syntax.Append(temp.ToString());
- }
- if ( m_ThisPropertyInfo.CanWrite && (m_ThisPropertyInfo.GetSetMethod(true) != null))
- {
- StringBuilder temp = new StringBuilder(m_Setter.HTMLDeclaration);
- temp.Replace(m_Setter.Attributes, PropertyAttributes(m_Setter.ThisMethodInfo));
- Syntax.Append(temp.ToString());
- }
-
- return Syntax.ToString();
- }
- }
-
- // 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 = "Assembly: ";
- 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 = "Assembly: ";
- 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 = "Assembly: ";
- else if ( m_Setter.ThisMethodInfo.IsFamilyOrAssembly )
- m_Access = "FamilyOrAssembly: ";
- else if ( m_Setter.ThisMethodInfo.IsFamilyAndAssembly )
- m_Access = "FamilyAndAssembly: ";
- }
- m_Access += "__property ";
- return m_Access;
- }
- }
-
- public string Attributes
- {
- override get { return this.Access; }
- }
-
- public override bool IsProperty()
- {
- return true;
- }
- }
-
- } // namespace LangUtil
-