home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: LangFieldInfo.cs
-
- Summary: LangFieldInfo implements or declares the functionality common
- to subclasses that represent a field in a particular language.
-
- ---------------------------------------------------------------------
- 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.Reflection;
- using System.Collections;
-
- public abstract class LangFieldInfo : LangMember
- {
- internal String m_Name = String.Empty;
- internal FieldInfo m_ThisFieldInfo = null;
- internal String m_Value = String.Empty;
-
- public LangFieldInfo( 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_ThisFieldInfo = myField;
-
- }
-
- public string FieldLangTypeName
- {
- abstract get;
- }
-
- /* The FieldLangType property must be implemented by subclasses to return
- // a LangType of the particular language that subclass represents. Implementing
- // it on the subclass means that the caller does not have to cast before
- // being able to use it.
-
- public <LanguageName>LangType FieldLangType{
- get{
- return m_FieldLangType;
- }
- }
- */
-
- public FieldInfo ThisFieldInfo
- {
- get { return m_ThisFieldInfo; }
- }
-
- // Note: Recall that if this type is a parameter or returntype, it
- // can be IsPointer, IsArray, or IsByRef.
-
- public Type ThisType
- {
- override get { return m_ThisFieldInfo.ReflectedType; }
- }
-
- public override bool IsField()
- {
- return true;
- }
-
- /*
- // FieldValue returns a string if a field value can be determined;
- // otherwise the property returns String.Empty. Note: Internal method
- // does not currently format the value to appear in the appropriate
- // way for a particular language. For example, hex values always appear 0xFFF.
- */
-
- public String FieldValue
- {
- get { return this.GetValueOfField(); }
- }
-
- public String Name
- {
- override get { return m_Name; }
- }
-
- /*
- // GetValueOfField takes no arguments and returns a string indicating a
- // specific value if it can be determined, or String.Empty if not.
- */
-
- internal 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 = String.Empty;
-
- 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
-