home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: VisualBasicFieldInfo.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 VisualBasicFieldInfo : LangMember
- {
- internal String m_Name = String.Empty;
- internal VisualBasicLangType m_FieldLangType = null;
- internal FieldInfo m_ThisFieldInfo = null;
-
- public VisualBasicFieldInfo( FieldInfo myField )
- {
- if ( myField == null )
- throw new NullReferenceException( "The FieldInfo passed to the constructor is null." );
-
- // Determine method name and return type
-
- m_Name = myField.Name;
- m_FieldLangType = new VisualBasicLangType( myField.FieldType );
- m_ThisFieldInfo = myField;
- }
-
- public string FieldLangTypeName
- {
- get { return m_FieldLangType.ClassName; }
- }
-
- public VisualBasicLangType FieldLangType
- {
- get { return m_FieldLangType; }
- }
-
- public FieldInfo ThisFieldInfo
- {
- get { return m_ThisFieldInfo; }
- }
-
- // If the value cannot be obtained, for whatever reason, an empty string is returned
- public string FieldValue
- {
- get { return this.GetValueOfField(); }
- }
-
- public Type ThisType
- {
- override get { return m_ThisFieldInfo.ReflectedType; }
- }
-
- public String Name
- {
- override get { return m_Name; }
- }
-
- public override bool IsField()
- {
- return true;
- }
-
-
- public String Attributes
- {
- override get
- {
- StringBuilder sb = new StringBuilder();
-
- if ( m_ThisFieldInfo.IsPublic )
- sb.Append( "Public " );
- else if ( m_ThisFieldInfo.IsPrivate )
- sb.Append( "Private " );
- else if ( m_ThisFieldInfo.IsFamily )
- sb.Append( "Protected " );
- else if ( m_ThisFieldInfo.IsAssembly )
- sb.Append( "Assembly " );
- else if ( m_ThisFieldInfo.IsFamilyAndAssembly )
- sb.Append( "FamilyAndAssembly " );
- else if ( m_ThisFieldInfo.IsFamilyOrAssembly )
- sb.Append( "FamilyOrAssembly " );
-
- if ( m_ThisFieldInfo.IsLiteral && m_ThisFieldInfo.IsStatic )
- sb.Append( "Const " );
-
- if ( m_ThisFieldInfo.IsStatic && !m_ThisFieldInfo.IsLiteral )
- sb.Append( "Shared " );
-
- return sb.ToString();
- }
- }
-
- public string TextDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder( Attributes + m_Name );
-
- Syntax.Append( " As " );
- Syntax.Append( m_FieldLangType.ClassName );
-
- string temp = GetValueOfField();
- if ( !temp.Equals( String.Empty ) )
- {
- Syntax.Append( " = " + temp );
- }
- 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>" );
- Syntax.Append( Attributes + m_Name );
- Syntax.Append( " As " );
- Syntax.Append( m_FieldLangType.ClassName );
-
- string temp = GetValueOfField();
- if ( !temp.Equals( String.Empty ) )
- {
- Syntax.Append( " = " + temp );
- }
-
- Syntax.Append( "</B></PRE>" );
- return Syntax.ToString();
- }
- }
-
-
- private string GetValueOfField()
- {
- if ( !( m_ThisFieldInfo.IsStatic && m_ThisFieldInfo.IsLiteral ) )
- return String.Empty;
-
- Object value;
- try
- {
- value = m_ThisFieldInfo.GetValue( m_ThisFieldInfo.DeclaringType );
- }
- catch
- {
- value = null;
- }
-
- string strValue = String.Empty;
-
- try
- {
- if ( value.GetType() == typeof( string ) )
- strValue = String.Format( "\"{0}\"", value.ToString() );
- else if ( value.GetType() == typeof( bool ) )
- strValue = value.ToString();
- else if ( value.GetType() == typeof( char ) )
- strValue = String.Format( "0x{0:X4}", Convert.ToInt32( value ) );
- else if ( m_ThisFieldInfo.DeclaringType.IsEnum )
- strValue = String.Format( "{0}", Convert.ToInt32( m_ThisFieldInfo.GetValue( null ) ) );
- else if ( value.GetType().IsPrimitive )
- strValue = String.Format( "{0}", value.ToString() );
- else
- strValue = String.Empty;
- }
- catch
- {
- strValue = String.Empty;
- }
- return strValue;
- }
- }
-
- } // namespace LangUtil
-