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

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