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

  1. /*=====================================================================
  2.   File:      VisualBasicFieldInfo.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 VisualBasicFieldInfo : LangMember
  29.    {
  30.       internal String m_Name = String.Empty;
  31.       internal VisualBasicLangType m_FieldLangType = null;
  32.       internal FieldInfo m_ThisFieldInfo = null;
  33.  
  34.       public VisualBasicFieldInfo( 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.        
  41.          m_Name = myField.Name;
  42.          m_FieldLangType = new VisualBasicLangType( myField.FieldType );
  43.          m_ThisFieldInfo = myField;      
  44.       }
  45.    
  46.       public string FieldLangTypeName
  47.       {
  48.          get { return m_FieldLangType.ClassName; }
  49.       }
  50.  
  51.       public VisualBasicLangType FieldLangType
  52.       {
  53.          get { return m_FieldLangType; }
  54.       }
  55.  
  56.       public FieldInfo ThisFieldInfo
  57.       {
  58.          get { return m_ThisFieldInfo; }
  59.       }
  60.  
  61.       // If the value cannot be obtained, for whatever reason, an empty string is returned
  62.       public string FieldValue
  63.       {
  64.          get { return this.GetValueOfField(); }
  65.       }     
  66.  
  67.       public Type ThisType
  68.       {
  69.          override get { return m_ThisFieldInfo.ReflectedType; }
  70.       }
  71.  
  72.       public String Name
  73.       {
  74.          override get { return m_Name; }
  75.       }
  76.  
  77.       public override bool IsField()
  78.       {
  79.          return true;
  80.       }
  81.  
  82.  
  83.       public String Attributes
  84.       {
  85.          override get
  86.          {
  87.             StringBuilder sb = new StringBuilder();
  88.             
  89.             if ( m_ThisFieldInfo.IsPublic )
  90.                sb.Append( "Public " );
  91.             else if ( m_ThisFieldInfo.IsPrivate )
  92.                sb.Append( "Private " );
  93.             else if ( m_ThisFieldInfo.IsFamily )
  94.                sb.Append( "Protected " );
  95.             else if ( m_ThisFieldInfo.IsAssembly )
  96.                sb.Append( "Assembly " );
  97.             else if ( m_ThisFieldInfo.IsFamilyAndAssembly )
  98.                sb.Append( "FamilyAndAssembly " );
  99.             else if ( m_ThisFieldInfo.IsFamilyOrAssembly )
  100.                sb.Append( "FamilyOrAssembly " );
  101.             
  102.             if ( m_ThisFieldInfo.IsLiteral && m_ThisFieldInfo.IsStatic )
  103.                sb.Append( "Const " );
  104.  
  105.             if ( m_ThisFieldInfo.IsStatic && !m_ThisFieldInfo.IsLiteral )
  106.                sb.Append( "Shared " );
  107.             
  108.             return sb.ToString();
  109.          }
  110.       }
  111.  
  112.       public string TextDeclaration
  113.       {
  114.          override get
  115.          {
  116.             StringBuilder Syntax = new StringBuilder( Attributes + m_Name );
  117.             
  118.             Syntax.Append( " As " );
  119.             Syntax.Append( m_FieldLangType.ClassName );
  120.             
  121.             string temp = GetValueOfField();
  122.             if ( !temp.Equals( String.Empty ) )
  123.             {
  124.                Syntax.Append( " = " + temp );
  125.             }
  126.             return Syntax.ToString();
  127.          }
  128.       }
  129.  
  130.       public string HTMLDeclaration
  131.       {
  132.          override get
  133.          {
  134.             StringBuilder Syntax = new StringBuilder( "<PRE class=\"Syntax\">" );
  135.             Syntax.Append( "<span style=\"color:red\">[Visual Basic]</span><BR><B>" );
  136.             Syntax.Append( Attributes + m_Name );
  137.             Syntax.Append( " As " );
  138.             Syntax.Append( m_FieldLangType.ClassName );
  139.                
  140.             string temp = GetValueOfField();
  141.             if ( !temp.Equals( String.Empty ) )
  142.             {
  143.                Syntax.Append( " = " + temp );
  144.             }
  145.  
  146.             Syntax.Append( "</B></PRE>" );
  147.             return Syntax.ToString();
  148.          }
  149.       }
  150.  
  151.  
  152.       private string GetValueOfField()
  153.       {
  154.          if ( !( m_ThisFieldInfo.IsStatic && m_ThisFieldInfo.IsLiteral ) )
  155.             return String.Empty;
  156.  
  157.          Object value;
  158.          try 
  159.          {
  160.             value = m_ThisFieldInfo.GetValue( m_ThisFieldInfo.DeclaringType );
  161.          } 
  162.          catch
  163.          {
  164.             value = null;
  165.          }
  166.  
  167.          string strValue = String.Empty;
  168.  
  169.          try 
  170.          {
  171.             if ( value.GetType() == typeof( string ) )
  172.                strValue = String.Format( "\"{0}\"", value.ToString() );
  173.             else if ( value.GetType() == typeof( bool ) )
  174.                strValue = value.ToString();
  175.             else if ( value.GetType() == typeof( char ) )
  176.                strValue = String.Format( "0x{0:X4}", Convert.ToInt32( value ) );
  177.             else if ( m_ThisFieldInfo.DeclaringType.IsEnum )
  178.                strValue = String.Format( "{0}", Convert.ToInt32( m_ThisFieldInfo.GetValue( null ) ) );
  179.             else if ( value.GetType().IsPrimitive )
  180.                strValue = String.Format( "{0}", value.ToString() );
  181.             else
  182.                strValue = String.Empty;
  183.          }
  184.          catch
  185.          {
  186.             strValue = String.Empty;
  187.          }
  188.          return strValue;
  189.       }
  190.    }
  191.  
  192. } // namespace LangUtil
  193.