home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: VisualBasicPropertyDetails.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.IO;
- using System.Text;
- using System.Reflection;
- using System.Collections;
-
- public class VisualBasicPropertyInfo : LangMember
- {
- internal string m_Name = String.Empty;
- internal PropertyInfo m_ThisPropertyInfo = null;
- internal VisualBasicLangType m_GetSetType = null;
- internal VisualBasicMethodInfo m_Getter = null;
- internal VisualBasicMethodInfo m_Setter = null;
- internal ArrayList m_Parameters = new ArrayList();
- internal String m_Access = String.Empty;
-
- public VisualBasicPropertyInfo( 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 VisualBasicMethodInfo(myProperty.GetGetMethod( true ) );
- if ( myProperty.CanWrite )
- m_Setter = new VisualBasicMethodInfo( myProperty.GetSetMethod( true ) );
-
- VisualBasicMethodInfo 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 VisualBasicLangType( tmpArray[i] ) );
-
- m_GetSetType = new VisualBasicLangType( 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 VisualBasicLangType( tmpArray[i] ) );
-
- // The last arg has same type as the type of this property
- m_GetSetType = new VisualBasicLangType( 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 at: " + Ex.TargetSite.Name, Ex );
- }
- }
-
- public Type ThisType
- {
- override get { return m_ThisPropertyInfo.ReflectedType; }
- }
-
- public String Name
- {
- override get { return m_Name; }
- }
-
- public VisualBasicLangType GetSetLangType
- {
- get { return m_GetSetType; }
- }
-
-
- public String TextDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder( String.Empty );
-
- if ( m_ThisPropertyInfo.CanRead )
- {
- Syntax.Append( Attributes + "Get " + m_Name + "(" );
-
- for ( int j = 0; j < m_Parameters.Count; j++ )
- {
- if ( ( (VisualBasicLangType)m_Parameters[j] ).IsByRef )
- Syntax.Append( "ByRef " );
- else
- Syntax.Append( "ByVal " );
-
- Syntax.Append( ( (VisualBasicLangType)m_Parameters[j] ).ParamVarName );
-
- if ( ( (VisualBasicLangType)m_Parameters[j] ).m_IsArray )
- Syntax.Append( "()" );
-
- Syntax.Append( " As " + ( (VisualBasicLangType)m_Parameters[j] ).ClassName );
-
- if ( j != ( m_Parameters.Count - 1 ) )
- Syntax.Append( ", " );
- }
-
- Syntax.Append( ") As " );
-
- if ( m_GetSetType.m_IsByRef )
- Syntax.Append( "ByRef " );
-
- Syntax.Append( m_GetSetType.ClassName );
-
- if ( m_GetSetType.m_IsArray )
- Syntax.Append( "()" );
- }
-
- if ( m_ThisPropertyInfo.CanWrite )
- {
- Syntax.Append( " " + Attributes );
-
- Type temptype = m_ThisPropertyInfo.PropertyType;
- if ( temptype.IsValueType || ( temptype == typeof( string ) ) )
- Syntax.Append( "Let " );
- else if ( ( temptype.IsArray && ( temptype != typeof( Array ) ) ) || ( !temptype.IsValueType && temptype != typeof( string ) ) )
- Syntax.Append( "Set " );
- else
- Syntax.Append( "Let " );
-
- Syntax.Append( m_Name );
- Syntax.Append( "(" );
-
- for ( int j = 0; j < m_Parameters.Count; j++ )
- {
- if ( ( (VisualBasicLangType)m_Parameters[j] ).IsByRef )
- Syntax.Append( "ByRef " );
- else
- Syntax.Append( "ByVal " );
-
- Syntax.Append( "" + ( (VisualBasicLangType)m_Parameters[j] ).ParamVarName );
-
- if ( ( (VisualBasicLangType)m_Parameters[j] ).m_IsArray )
- Syntax.Append( "()" );
-
- Syntax.Append( " As " + ( (VisualBasicLangType)m_Parameters[j] ).ClassName );
- Syntax.Append( ", " );
- }
-
- Syntax.Append( "ByVal value" );
- if ( temptype.IsArray )
- Syntax.Append( "()" );
- Syntax.Append( " As " );
-
- // Not yet implemented: ClassName will not be the name of the class if it's HasElementType.
- Syntax.Append( m_GetSetType.ClassName );
- Syntax.Append( ")" );
- }
- return Syntax.ToString();
- }
- }
-
- public String HTMLDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
- Syntax.Append( "<span style=\"color:red\">[Visual Basic]</span><BR><B>" );
- if ( m_ThisPropertyInfo.CanRead )
- {
- Syntax.Append( Attributes );
- Syntax.Append( "Get " );
- Syntax.Append( m_Name );
- Syntax.Append( "(" );
- for ( int j = 0; j < m_Parameters.Count; j++ )
- {
- if ( ( (VisualBasicLangType)m_Parameters[j]).IsByRef )
- Syntax.Append( "ByRef <I>" );
- else
- Syntax.Append( "ByVal <I>" );
- Syntax.Append( ( (VisualBasicLangType)m_Parameters[j] ).ParamVarName + "</I>" );
- if ( ( (VisualBasicLangType)m_Parameters[j] ).m_IsArray )
- Syntax.Append( "()" );
- Syntax.Append( " As " + ( (VisualBasicLangType)m_Parameters[j] ).ClassName );
- if ( j != ( m_Parameters.Count - 1 ) )
- {
- Syntax.Append( ", " );
- }
- }
- Syntax.Append( ")" );
- Syntax.Append( " As " );
- if ( m_GetSetType.m_IsByRef )
- Syntax.Append( "ByRef " );
- Syntax.Append( m_GetSetType.ClassName );
- if ( m_GetSetType.m_IsArray )
- Syntax.Append( "()" );
- }
-
- if ( m_ThisPropertyInfo.CanWrite )
- {
- if ( m_ThisPropertyInfo.CanRead )
- Syntax.Append( "<BR>" );
- Syntax.Append( Attributes );
- Type temptype = m_GetSetType.ThisType;
-
- if ( temptype.IsValueType || temptype == typeof( string ) )
- Syntax.Append( "Let " );
- else if ( ( temptype.IsArray && ( temptype != typeof( Array ) ) ) ||
- (!temptype.IsValueType && temptype!= typeof( System.String ) ) )
- Syntax.Append( "Set " );
- else
- Syntax.Append( "Let " );
-
- temptype = null;
- Syntax.Append( m_Name );
- Syntax.Append( "(" );
- for ( int j = 0; j < m_Parameters.Count; j++ )
- {
- if ( ( (VisualBasicLangType)m_Parameters[j] ).IsByRef )
- Syntax.Append( "ByRef " );
- else
- Syntax.Append( "ByVal " );
- Syntax.Append( "<I>" + ( (VisualBasicLangType)m_Parameters[j] ).ParamVarName + "</I>" );
- if ( ( (VisualBasicLangType)m_Parameters[j] ).m_IsArray )
- Syntax.Append( "()" );
- Syntax.Append( " As " + ( (VisualBasicLangType)m_Parameters[j] ).ClassName );
- Syntax.Append( ", " );
- }
- Syntax.Append( "ByVal <I>value</I> As " );
- Syntax.Append( m_GetSetType.ClassName );
- Syntax.Append( ")" );
- }
- Syntax.Append( "</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 and exception.
-
- protected String Access
- {
- get
- {
- string m_Access = String.Empty;
-
- // Determine if this is a default property
- MemberInfo[] mi = ThisType.GetDefaultMembers();
- if ( mi != null )
- {
- for ( int i = 0; i < mi.Length; i++ )
- {
- if ( mi[i] == m_ThisPropertyInfo )
- {
- m_Access += "Default ";
- break;
- }
- }
- }
-
- 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 do not have the same level of access on the "
- + m_ThisPropertyInfo.ReflectedType.FullName + "."
- + m_ThisPropertyInfo.Name
- + " property." );
- }
- 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 ";
- }
- return m_Access;
- }
- }
-
- public string Attributes
- {
- override get
- {
- StringBuilder sb = new StringBuilder( this.Access );
- sb.Append( "Property " );
- return sb.ToString();
- }
- }
-
- public override bool IsProperty()
- {
- return true;
- }
- }
-
- } // namespace LangUtil
-