home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / ClsView / MCFieldInfo.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  6.0 KB  |  192 lines

  1. /*=====================================================================
  2.   File:      MCFieldInfo.cs
  3.  
  4.   Summary:   Brief summary of the file contents and purpose.
  5.  
  6. ---------------------------------------------------------------------
  7.   This file is part of the Microsoft NGWS SDK Code Samples.
  8.  
  9.   Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  10.  
  11. This source code is intended only as a supplement to Microsoft
  12. Development Tools and/or on-line documentation.  See these other
  13. materials for detailed information regarding Microsoft code samples.
  14.  
  15. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. PARTICULAR PURPOSE.
  19. =====================================================================*/
  20.  
  21. namespace LangUtil {
  22.  
  23. using System;
  24. using System.Text;
  25. using System.Reflection;
  26. using System.Collections;
  27.  
  28.    public class MCFieldInfo : LangMember
  29.    {
  30.       internal String m_Name = String.Empty;
  31.       internal MCLangType m_FieldLangType = null;
  32.       internal FieldInfo m_ThisFieldInfo = null;
  33.  
  34.       public MCFieldInfo( FieldInfo myField )
  35.       {
  36.          if ( myField == null )
  37.             throw new NullReferenceException( "The FieldInfo passed to the constructor is null." );
  38.  
  39.          // Determine method name and return type       
  40.          m_Name = myField.Name;
  41.          m_FieldLangType = new MCLangType( myField.FieldType );
  42.          m_ThisFieldInfo = myField;
  43.       }
  44.    
  45.       public string FieldLangTypeName
  46.       {
  47.          get { return m_FieldLangType.ClassName; }
  48.       }
  49.  
  50.       public MCLangType FieldLangType
  51.       {
  52.          get { return m_FieldLangType; }
  53.       }
  54.  
  55.       public FieldInfo ThisFieldInfo
  56.       {
  57.          get { return m_ThisFieldInfo; }
  58.       }
  59.  
  60.       // If the value cannot be obtained, for whatever reason, an empty string is returned
  61.       public string FieldValue
  62.       {
  63.          get { return this.GetValueOfField(); }
  64.       }     
  65.  
  66.       public String Name
  67.       {
  68.          override get { return m_Name; }
  69.       }
  70.  
  71.       public Type ThisType
  72.       {
  73.          override get { return m_ThisFieldInfo.ReflectedType; }
  74.       }
  75.  
  76.       public override bool IsField()
  77.       {
  78.          return true;
  79.       }
  80.  
  81.       public string TextDeclaration
  82.       {
  83.          override get
  84.          {
  85.             StringBuilder Syntax = new StringBuilder( this.Attributes + m_FieldLangType.ClassName );
  86.             Syntax.Append( " " );
  87.             Syntax.Append( m_Name );
  88.             string temp = GetValueOfField();
  89.             if ( !temp.Equals( String.Empty ) )
  90.             {
  91.                Syntax.Append( " = " + temp );
  92.             }
  93.             Syntax.Append( ";" );
  94.             return Syntax.ToString();
  95.          }
  96.       }
  97.  
  98.       public string HTMLDeclaration
  99.       {
  100.          override get
  101.          {
  102.             StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
  103.             Syntax.Append( "<span style=\"color:red\">[MC++]</span><BR><B>" );
  104.             Syntax.Append( this.Attributes + m_FieldLangType.ClassName );
  105.             Syntax.Append( " " );
  106.             Syntax.Append( m_Name );
  107.  
  108.             string temp = GetValueOfField();
  109.             if ( !temp.Equals( String.Empty ) )
  110.             {
  111.                Syntax.Append( " = " + temp );
  112.             }
  113.             Syntax.Append( ";" );
  114.             Syntax.Append( "</B></PRE>" );
  115.             return Syntax.ToString();
  116.          }
  117.       }
  118.  
  119.       public String Attributes
  120.       {
  121.          override get
  122.          {
  123.             StringBuilder sb = new StringBuilder();
  124.             
  125.             // Append access modifier
  126.             if ( m_ThisFieldInfo.IsPublic )
  127.                sb.Append( "public: " );
  128.             else if ( m_ThisFieldInfo.IsPrivate )
  129.                sb.Append( "private: " );
  130.             else if ( m_ThisFieldInfo.IsFamily )
  131.                sb.Append( "protected: " );
  132.             else if ( m_ThisFieldInfo.IsAssembly )
  133.                sb.Append( "Assembly: " );
  134.             else if ( m_ThisFieldInfo.IsFamilyAndAssembly )
  135.                sb.Append( "FamilyAndAssembly: " );
  136.             else if ( m_ThisFieldInfo.IsFamilyOrAssembly )
  137.                sb.Append( "FamilyOrAssembly: " );
  138.             
  139.             if ( m_ThisFieldInfo.IsLiteral && m_ThisFieldInfo.IsStatic )
  140.                sb.Append( "const static " );
  141.             else if ( m_ThisFieldInfo.IsInitOnly )
  142.                sb.Append( "const " );
  143.             // Since static is also written above, we check for !Literal
  144.             if ( m_ThisFieldInfo.IsStatic && !m_ThisFieldInfo.IsLiteral )
  145.                sb.Append( "static " );
  146.             
  147.             return sb.ToString();
  148.          }
  149.       }
  150.  
  151.       private string GetValueOfField()
  152.       {
  153.          if ( !( m_ThisFieldInfo.IsStatic && m_ThisFieldInfo.IsLiteral ) ) 
  154.             return String.Empty;
  155.  
  156.          Object value;
  157.          try 
  158.          {
  159.             value = m_ThisFieldInfo.GetValue( m_ThisFieldInfo.DeclaringType );
  160.          } 
  161.          catch
  162.          {
  163.             value = null;
  164.          }
  165.  
  166.          string strValue;
  167.  
  168.          try
  169.          {
  170.             if ( value.GetType() == typeof( string ) )
  171.                strValue = String.Format( "\"{0}\"", value.ToString() );
  172.             else if ( value.GetType() == typeof( bool ) )
  173.                strValue = value.ToString();
  174.             else if ( value.GetType() == typeof( Char ) )
  175.                strValue = String.Format( "0x{0:X4}", Convert.ToInt32( value ) );
  176.             else if ( m_ThisFieldInfo.DeclaringType.IsEnum )
  177.                strValue = String.Format( "{0}", Convert.ToInt32( m_ThisFieldInfo.GetValue( null ) ) );
  178.             else if ( value.GetType().IsPrimitive )
  179.                strValue = String.Format( "{0}", value.ToString() );
  180.             else
  181.                strValue = String.Empty;
  182.          }
  183.          catch
  184.          {            
  185.             strValue = String.Empty;
  186.          }
  187.          return strValue;
  188.        }
  189.    }
  190.  
  191. } // namespace LangUtil
  192.