home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: NativeFieldInfo.cs
-
- Summary: Brief summary of the file contents and purpose.
-
- ---------------------------------------------------------------------
- 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.Text;
- using System.Reflection;
- using System.Collections;
-
- public class NativeFieldInfo : LangMember
- {
- internal String m_Name = String.Empty;
- internal NativeLangType m_FieldLangType = null;
- internal FieldInfo m_ThisFieldInfo = null;
- internal String m_Value = String.Empty;
-
- public NativeFieldInfo( 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_FieldLangType = new NativeLangType( myField.FieldType );
- m_ThisFieldInfo = myField;
- }
-
- public String FieldLangTypeName
- {
- get { return m_FieldLangType.ClassName; }
- }
-
- public NativeLangType FieldLangType
- {
- get { return m_FieldLangType; }
- }
-
- public VisualBasicFieldInfo VisualBasicLangObject
- {
- get { return new VisualBasicFieldInfo( m_ThisFieldInfo ); }
- }
-
- public CSharpFieldInfo CSharpLangObject
- {
- get { return new CSharpFieldInfo( m_ThisFieldInfo ); }
- }
-
- public MCFieldInfo MCLangObject
- {
- get { return new MCFieldInfo( m_ThisFieldInfo ); }
- }
-
- public FieldInfo ThisFieldInfo
- {
- get { return m_ThisFieldInfo; }
- }
-
- public Type ThisType
- {
- override get { return m_ThisFieldInfo.ReflectedType; }
- }
-
- public override bool IsField()
- {
- return true;
- }
-
- // If the value cannot be obtained, for whatever reason, an empty string is returned
- public string FieldValue
- {
- get { return this.GetValueOfField(); }
- }
-
- public String Name
- {
- override get { return m_Name; }
- }
-
- /*
- public string TextDeclaration{
- get{
- throw new NotSupportedException("TextFieldDeclaration is not yet implemented in NativeFieldInfo.");
- StringBuilder Syntax = new StringBuilder (this.NativeAttributes + m_FieldLangType.ClassName);
- Syntax.Append(" ");
- Syntax.Append(m_Name);
- Syntax.Append(";");
- string temp = GetValueOfField();
- if (!temp.Equals(""))
- Syntax.Append(" // value = " + temp);
- return Syntax.ToString();
- }
- }
-
- public string HTMLFieldDeclaration{
- get{
- throw new NotSupportedException("HTMLFieldDeclaration is not yet implemented in NativeFieldInfo.");
- StringBuilder Syntax = new StringBuilder ("<PRE class=\"Syntax\">");
- Syntax.Append("<span style=\"color:red\">[CSharp]</span><BR><B>");
- Syntax.Append(this.NativeAttributes);
- Syntax.Append(m_FieldLangType.ClassName);
- Syntax.Append(" ");
- Syntax.Append(m_Name);
-
- Syntax.Append(";");
- string temp = GetValueOfField();
- if (!temp.Equals(""))
- Syntax.Append(" // value = " + temp);
- Syntax.Append("</B></PRE>");
- return Syntax.ToString();
- }
- }
- */
-
- public String Attributes
- {
- override get
- {
- StringBuilder sb = new StringBuilder();
-
- // Append access modifier
- if ( m_ThisFieldInfo.IsPublic )
- sb.Append( "public " );
- else if ( m_ThisFieldInfo.IsPrivate )
- sb.Append( "private " );
- else if ( m_ThisFieldInfo.IsFamily )
- sb.Append( "protected " );
- else if ( m_ThisFieldInfo.IsAssembly )
- sb.Append( "internal " );
- else if ( m_ThisFieldInfo.IsFamilyAndAssembly )
- sb.Append( "FamilyAndAssembly " );
- else if ( m_ThisFieldInfo.IsFamilyOrAssembly )
- sb.Append( "FamilyOrAssembly " );
-
- if ( m_ThisFieldInfo.IsLiteral && m_ThisFieldInfo.IsStatic )
- sb.Append( "const " );
-
- // If both Literal and InitOnly and true then const and InitOnly are written
- if ( m_ThisFieldInfo.IsInitOnly )
- sb.Append( "readonly " );
- if ( m_ThisFieldInfo.IsStatic && !m_ThisFieldInfo.IsLiteral )
- sb.Append( "static " );
-
- return sb.ToString();
- }
- }
-
- private 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
-