home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: MCFieldInfo.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 MCFieldInfo : LangMember
- {
- internal String m_Name = String.Empty;
- internal MCLangType m_FieldLangType = null;
- internal FieldInfo m_ThisFieldInfo = null;
-
- public MCFieldInfo( 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 MCLangType( myField.FieldType );
- m_ThisFieldInfo = myField;
- }
-
- public string FieldLangTypeName
- {
- get { return m_FieldLangType.ClassName; }
- }
-
- public MCLangType 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 String Name
- {
- override get { return m_Name; }
- }
-
- public Type ThisType
- {
- override get { return m_ThisFieldInfo.ReflectedType; }
- }
-
- public override bool IsField()
- {
- return true;
- }
-
- public string TextDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder( this.Attributes + m_FieldLangType.ClassName );
- Syntax.Append( " " );
- Syntax.Append( m_Name );
- string temp = GetValueOfField();
- if ( !temp.Equals( String.Empty ) )
- {
- Syntax.Append( " = " + temp );
- }
- Syntax.Append( ";" );
- return Syntax.ToString();
- }
- }
-
- public string HTMLDeclaration
- {
- override get
- {
- StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
- Syntax.Append( "<span style=\"color:red\">[MC++]</span><BR><B>" );
- Syntax.Append( this.Attributes + m_FieldLangType.ClassName );
- Syntax.Append( " " );
- Syntax.Append( m_Name );
-
- string temp = GetValueOfField();
- if ( !temp.Equals( String.Empty ) )
- {
- Syntax.Append( " = " + temp );
- }
- Syntax.Append( ";" );
- Syntax.Append( "</B></PRE>" );
- return Syntax.ToString();
- }
- }
-
- public String Attributes
- {
- override get
- {
- StringBuilder sb = new StringBuilder();
-
- // Append access modifier
- 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 static " );
- else if ( m_ThisFieldInfo.IsInitOnly )
- sb.Append( "const " );
- // Since static is also written above, we check for !Literal
- if ( m_ThisFieldInfo.IsStatic && !m_ThisFieldInfo.IsLiteral )
- sb.Append( "static " );
-
- return sb.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;
-
- 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
-