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

  1. /*=====================================================================
  2.   File:      LangFieldInfo.cs
  3.  
  4.   Summary:   LangFieldInfo implements or declares the functionality common
  5.          to subclasses that represent a field in a particular language.
  6.  
  7. ---------------------------------------------------------------------
  8.   This file is part of the Microsoft NGWS SDK Code Samples.
  9.  
  10.   Copyright (C) 2000 Microsoft Corporation.  All rights reserved.
  11.  
  12. This source code is intended only as a supplement to Microsoft
  13. Development Tools and/or on-line documentation.  See these other
  14. materials for detailed information regarding Microsoft code samples.
  15.  
  16. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  17. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  19. PARTICULAR PURPOSE.
  20. =====================================================================*/
  21.  
  22. namespace LangUtil {
  23.  
  24. using System;
  25. using System.Reflection;
  26. using System.Collections;
  27.  
  28.    public abstract class LangFieldInfo : LangMember
  29.    {
  30.       internal String m_Name = String.Empty;
  31.       internal FieldInfo m_ThisFieldInfo = null;
  32.       internal String m_Value = String.Empty;
  33.  
  34.       public LangFieldInfo( 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_ThisFieldInfo = myField;
  42.  
  43.       }
  44.    
  45.       public string FieldLangTypeName
  46.       {
  47.          abstract get;
  48.       }
  49.  
  50.       /* The FieldLangType property must be implemented by subclasses to return
  51.       // a LangType of the particular language that subclass represents. Implementing
  52.       // it on the subclass means that the caller does not have to cast before 
  53.       // being able to use it.
  54.  
  55.       public <LanguageName>LangType FieldLangType{
  56.          get{ 
  57.             return m_FieldLangType; 
  58.          } 
  59.       }
  60.       */
  61.  
  62.       public FieldInfo ThisFieldInfo
  63.       {
  64.          get { return m_ThisFieldInfo; }
  65.       }
  66.  
  67.       // Note: Recall that if this type is a parameter or returntype, it 
  68.       // can be IsPointer, IsArray, or IsByRef.
  69.          
  70.       public Type ThisType
  71.       {
  72.          override get { return m_ThisFieldInfo.ReflectedType; }
  73.       }
  74.  
  75.       public override bool IsField()
  76.       {
  77.          return true;
  78.       }
  79.  
  80.       /*
  81.       // FieldValue returns a string if a field value can be determined; 
  82.       // otherwise the property returns String.Empty. Note: Internal method 
  83.       // does not currently format the value to appear in the appropriate 
  84.       // way for a particular language. For example, hex values always appear 0xFFF.
  85.       */
  86.  
  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.       // GetValueOfField takes no arguments and returns a string indicating a 
  99.       // specific value if it can be determined, or String.Empty if not.
  100.       */
  101.  
  102.       internal String GetValueOfField()
  103.       {
  104.          if ( !( m_ThisFieldInfo.IsStatic && m_ThisFieldInfo.IsLiteral ) ) 
  105.             return String.Empty;
  106.          Object value;
  107.          try 
  108.          {
  109.             value = m_ThisFieldInfo.GetValue( m_ThisFieldInfo.DeclaringType );
  110.          } 
  111.          catch
  112.          {
  113.             value = null;
  114.          }
  115.  
  116.          string strValue = String.Empty;
  117.  
  118.          try 
  119.          {
  120.             if ( value.GetType() == typeof( string ) )
  121.                strValue = String.Format( "\"{0}\"", value.ToString() );
  122.             else if ( value.GetType() == typeof( bool ) )
  123.                strValue = value.ToString();
  124.             else if ( value.GetType() == typeof( char ) )
  125.                strValue = String.Format( "0x{0:X4}", Convert.ToInt32( value ) );
  126.             else if ( m_ThisFieldInfo.DeclaringType.IsEnum )
  127.                strValue = String.Format( "{0}", Convert.ToInt32( m_ThisFieldInfo.GetValue( null ) ) );               
  128.             else if ( value.GetType().IsPrimitive )
  129.                strValue = String.Format( "{0}", value.ToString() );
  130.             else
  131.                strValue = String.Empty;
  132.          }
  133.          catch
  134.          {
  135.             strValue = String.Empty;
  136.          }
  137.          return strValue;
  138.       }     
  139.  
  140.    }
  141.  
  142. } // namespace LangUtil
  143.