home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: CSharpPropertyInfo.cs
-
- Summary: Extends LangPropertInfo to provide a CSharp-specific version
- of reflection for ease of use in a CSharp-based environment.
-
- ---------------------------------------------------------------------
- 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 CSharpPropertyInfo : LangPropertyInfo
- {
- public CSharpPropertyInfo( PropertyInfo myProperty ) : base( myProperty )
- {
- }
-
- public CSharpMethodInfo Getter
- {
- get
- {
- try
- {
- if ( m_ThisPropertyInfo.CanRead )
- {
- return new CSharpMethodInfo( m_ThisPropertyInfo.GetGetMethod( true ) );
- }
- else return null;
- }
- catch
- {
- throw new NullReferenceException( "Although "
- + this.ThisType.Name
- + "."
- + m_ThisPropertyInfo.Name
- + " says it is CanRead,"
- + " GetGetMethod(true) returns a null MethodInfo." );
- }
- }
- }
-
- public CSharpMethodInfo Setter
- {
- get
- {
- try
- {
- if ( m_ThisPropertyInfo.CanWrite )
- {
- return new CSharpMethodInfo( m_ThisPropertyInfo.GetSetMethod( true ) );
- }
- else return null;
- }
- catch
- {
- throw new NullReferenceException( "Although "
- + this.ThisType.Name
- + "."
- + m_ThisPropertyInfo.Name
- + " says it is CanWrite,"
- + " GetSetMethod(true) returns a null MethodInfo." );
- }
- }
- }
-
- public CSharpLangType GetSetLangType
- {
- get
- {
- try
- {
- if ( m_ThisPropertyInfo.CanWrite )
- {
- return new CSharpLangType( m_ThisPropertyInfo.GetGetMethod( true ).ReturnType );
- }
- else return null;
- }
- catch
- {
- throw new NullReferenceException( "Although "
- + this.ThisType.Name
- + "."
- + m_ThisPropertyInfo.Name
- + " says it is CanWrite,"
- + " GetSetMethod(true) returns a null MethodInfo." );
- }
- }
- }
-
- public String TextDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder( Access );
-
- if ( m_ThisPropertyInfo.CanRead )
- {
- if ( m_Getter.Attributes.IndexOf( "override" ) > -1 )
- Syntax.Append( "override " );
- else if ( m_Getter.Attributes.IndexOf( "abstract" ) > -1 )
- Syntax.Append( "abstract " );
- else if ( m_Getter.Attributes.IndexOf( "virtual" ) > -1 )
- Syntax.Append( "virtual " );
- }
- else if ( m_ThisPropertyInfo.CanWrite )
- {
- if ( m_Setter.Attributes.IndexOf( "override" ) > -1 )
- Syntax.Append( "override " );
- else if ( m_Setter.Attributes.IndexOf( "abstract" ) > -1 )
- Syntax.Append( "abstract " );
- else if ( m_Setter.Attributes.IndexOf( "virtual" ) > -1 )
- Syntax.Append( "virtual " );
- }
-
- if ( m_GetSetType.m_IsByRef )
- Syntax.Append( "ref " );
- Syntax.Append( m_GetSetType.ClassName );
- if ( m_GetSetType.m_IsArray )
- Syntax.Append( "[]" );
-
- Syntax.Append( " " );
-
- if ( m_Name.Equals( "Item" ) )
- Syntax.Append( "this" );
- else
- Syntax.Append( m_Name );
-
- if ( m_Parameters.Count > 0 )
- Syntax.Append( "[" );
-
- for ( int j = 0; j < m_Parameters.Count; j++ )
- {
- if ( ( (CSharpLangType)m_Parameters[j] ).m_IsByRef )
- Syntax.Append( "ref " );
- Syntax.Append( ( (CSharpLangType)m_Parameters[j] ).ClassName );
- if ( ((CSharpLangType)m_Parameters[j] ).m_IsArray )
- Syntax.Append( "[]" );
- /* if (((CSharpLangType)m_Parameters[j]).m_IsPointer)
- resolve Syntax.Append("*");
- */
- Syntax.Append( " " + ( (CSharpLangType)m_Parameters[j]).ParamVarName );
-
- if ( j != ( m_Parameters.Count - 1 ) )
- {
- Syntax.Append( ", " );
- }
- else
- {
- Syntax.Append( "]" );
- }
- }
-
- Syntax.Append( " {" );
- if ( m_ThisPropertyInfo.CanRead )
- Syntax.Append( "get;" );
- if ( m_ThisPropertyInfo.CanWrite )
- Syntax.Append( " set;" );
- Syntax.Append( "}" );
-
- return Syntax.ToString();
- }
- }
-
- public string HTMLDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
- Syntax.Append( "<span style=\"color:red\">[CSharp]</span><BR/><B>" );
- Syntax.Append( Access );
-
- if ( m_GetSetType.m_IsByRef )
- Syntax.Append( "ref " );
- Syntax.Append( m_GetSetType.ClassName );
- if ( m_GetSetType.m_IsArray )
- Syntax.Append( "[]" );
-
- Syntax.Append( " " );
- if ( m_Name.Equals( "Item" ) )
- Syntax.Append( "this" );
- else
- Syntax.Append( m_Name );
- if ( m_Parameters.Count > 0 )
- Syntax.Append( "[" );
- for ( int j = 0; j < m_Parameters.Count; j++ )
- {
- if ( ( (CSharpLangType)m_Parameters[j] ).m_IsByRef )
- Syntax.Append( "ref " );
- Syntax.Append( ( (CSharpLangType)m_Parameters[j] ).ClassName );
- if ( ( (CSharpLangType)m_Parameters[j] ).m_IsArray )
- Syntax.Append( "[]" );
- /* if (((CSharpLangType)m_Parameters[j]).m_IsPointer)
- resolve Syntax.Append("*");
- */
- // write the variable name.
- Syntax.Append( " <I>" + ( (CSharpLangType)m_Parameters[j] ).ParamVarName + "</I>" );
- if ( j != ( m_Parameters.Count - 1 ) )
- {
- Syntax.Append( ", " );
- }
- else
- {
- Syntax.Append( "]" );
- }
- }
- Syntax.Append( " {<BR/> " );
- if ( m_ThisPropertyInfo.CanRead )
- {
- if ( m_Getter.Attributes.IndexOf( "override" ) > -1 )
- Syntax.Append( "override " );
- else if ( m_Getter.Attributes.IndexOf( "abstract" ) > -1 )
- Syntax.Append( "abstract " );
- else if ( m_Getter.Attributes.IndexOf( "virtual" ) > -1 )
- Syntax.Append( "virtual " );
- Syntax.Append( "get;" );
- }
- if ( m_ThisPropertyInfo.CanWrite )
- {
- Syntax.Append( "<BR/> " );
- if ( m_Setter.Attributes.IndexOf( "override" ) > -1 )
- Syntax.Append( "override " );
- else if ( m_Setter.Attributes.IndexOf( "abstract" ) > -1 )
- Syntax.Append( "abstract " );
- else if ( m_Setter.Attributes.IndexOf( "virtual" ) > -1 )
- Syntax.Append( "virtual " );
- Syntax.Append( "set;" );
- }
- Syntax.Append( "<BR/>}</B></PRE>" );
- 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
- {
- override 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; }
- }
- }
-
- } // namespace LangUtil
-
-