home *** CD-ROM | disk | FTP | other *** search
- /*=====================================================================
- File: LangType.cs
-
- Summary: The LangType is the abstract base class that provides
- language-based translation and basic heirarchy and
- ordering functionality for the LangUtil namespace. Constructors
- take a Type or a ParameterInfo and either implement or
- declare the members necessary for callers to obtain
- easily language-specific information. Many members must be
- overridden; others are declared in comments, but implemented
- on children to avoid undue casting by callers.
-
- ---------------------------------------------------------------------
- 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.IO;
- using System.Text;
- using System.Reflection;
- using System.Collections;
-
- public abstract class LangType
- {
- internal Type m_TypeObject = null;
- internal string Type = String.Empty;
- internal string ShortType = String.Empty;
- public string ParamVarName = String.Empty;
- internal bool m_IsByRef = false;
- internal ParameterInfo m_ParamInfo = null;
- internal bool m_IsPointer = false;
- internal bool m_IsArray = false;
-
- internal int m_ACCESS_STRING_SIZE = 40;
- internal int m_ATTRIBUTE_STRING_SIZE = 60;
-
- // Constructor takes a Type object and loads the necessary internal variables for
- // children to translate the object easily.
-
- public LangType( Type ThisType )
- {
- try
- {
- if ( ThisType == null )
- throw new NullReferenceException( "The Type passed to the constructor is null." );
-
- m_TypeObject = ThisType;
-
- if ( m_TypeObject.IsArray && ( m_TypeObject != typeof( Array ) ) )
- {
- Type = GetTypeString( m_TypeObject, true );
- ShortType = GetTypeString( m_TypeObject, false );
- }
- else
- {
- Type = m_TypeObject.FullName;
- ShortType = m_TypeObject.Name;
- }
- }
- catch ( Exception Ex )
- {
- throw new Exception( "Error in LangType constructor." , Ex );
- }
- }
-
- // Constructor takes a ParameterInfo. Extra work here includes loading the boolean flags that
- // indicate whether the ParameterInfo IsByRef, IsPointer, or IsArray
- public LangType( ParameterInfo ParamInfo )
- {
- try
- {
- m_ParamInfo = ParamInfo;
- m_TypeObject = ParamInfo.ParameterType;
- ParamVarName = ParamInfo.Name;
-
- Type = GetTypeString( m_TypeObject, true );
- ShortType = GetTypeString( m_TypeObject, false );
-
- // ByReference information for parameters
- m_IsByRef = ParamInfo.ParameterType.IsByRef;
- m_IsPointer = ParamInfo.ParameterType.IsPointer;
- m_IsArray = ParamInfo.ParameterType.IsArray;
- }
- catch ( Exception Ex )
- {
- throw new Exception( "Error in LangType constructor at: " /*+ Ex.GetMethod().Name*/, Ex );
- }
- }
-
- /*
- //
- // This always returns
- // the appropriate <namespace>.<className> form for all types, regardless of
- // their underlying elements, with one exception: It doesn't properly handle
- // multidimensional arrays, which it returns instead as "System.Array".
- //
- // This method is called by the child constructor to set the name for parameter types
- // and return types properly; usually, the child will call this and then add the
- // appropriate *, &, or other markers appropriate to the language, and return that
- // language-specific value through the FullClassName or ClassName properties.
- //
- */
-
- internal string GetTypeString( Type ThisType, bool WantFullName )
- {
- StringBuilder TypeString = new StringBuilder( String.Empty );
-
- if ( ThisType.HasElementType )
- {
- if ( ThisType.IsArray && !( ThisType.Name.Equals( "Array" ) ) )
- {
- m_IsArray = true;
- if ( WantFullName )
- TypeString.Append( ThisType.GetElementType().FullName );
- else
- TypeString.Append( ThisType.GetElementType().Name );
- }
- else if ( ThisType.Name.Equals( "Array" ) )
- {
- if ( WantFullName )
- TypeString.Append( "System.Array" );
- else
- TypeString.Append( "Array" );
-
- }
- else if ( ThisType.IsByRef )
- {
- if ( WantFullName )
- TypeString.Append( ThisType.GetElementType().FullName );
- else
- TypeString.Append( ThisType.GetElementType().Name );
- }
- else
- {
- if ( WantFullName )
- TypeString.Append( ThisType.GetElementType().FullName );
- else
- TypeString.Append( ThisType.GetElementType().Name );
- }
- }
- else
- {
- if ( WantFullName )
- TypeString.Append( ThisType.FullName );
- else
- TypeString.Append( ThisType.Name );
- }
- return TypeString.ToString();
- }
-
- // Default implementation does nothing
- virtual public string TranslateTypeName( string name )
- {
- return name;
- }
-
- // Returns the fully-qualified classname.
- public String FullClassName
- {
- virtual get { return Type; }
- }
-
- public String ClassName
- {
- get { return TranslateTypeName( ShortType ); }
- }
-
- public bool IsByRef
- {
- virtual get { return m_IsByRef; }
- }
-
- public bool IsPointer
- {
- virtual get { return m_IsPointer; }
- }
-
- public bool IsArray
- {
- virtual get { return m_IsArray; }
- }
-
- public bool IsRealArray
- {
- virtual get { return ( m_IsArray && ThisType != typeof( Array ) ) ? true : false; }
- }
-
- public Type ThisType
- {
- virtual get{ return m_TypeObject; }
- }
-
- /*
- // TextDeclaration must be overridden by the child classes to return a string that
- // is a legal declaration in the language of the child class to which the caller has
- // a reference.
- */
- public string TextDeclaration
- {
- abstract get;
- }
-
- /*
- // HTMLDeclaration must be overridden by the child classes to return a string that
- // is a legal declaration in the language of the child class to which the caller has
- // a reference.
- */
- public string HTMLDeclaration
- {
- abstract get;
- }
-
- /*
- // XMLDeclaration must be overridden by the child classes to return a string that
- // is a legal declaration in the language of the child class to which the caller has
- // a reference.
-
- Right now, however, there is no implementation.
- */
- public string XMLDeclaration
- {
- abstract get;
- }
-
- /*
- // OrderedConstructors returns an array of ConstructorInfos that are all the
- // constructorinfos returned by the Type passed to this class's constructor,
- // sorted by name. Right now, this sort only moves .cctor to the front of the
- // list followed by .ctor.
- // Property returns an array of all constructors, including .cctor. Caller is
- // responsible for stripping out any constructors that are unwanted.
- */
-
- public ConstructorInfo[] OrderedConstructors
- {
- virtual get
- {
- ConstructorInfo[] Constructors = null;
-
- try
- {
- Constructors = m_TypeObject.GetConstructors( BindingFlags.LookupAll );
- ConstructorComparer ICompConst = new ConstructorComparer();
- Array.Sort( Constructors, ICompConst );
- }
- catch ( Exception Ex )
- {
- throw new Exception( "Occurred in LangType.OrderedConstructors.", Ex );
- }
- return Constructors;
- }
- }
-
- /*
- // OrderedMethods returns an array of MethodInfos that are all the
- // MethodInfos returned by the Type passed to this class's constructor, sorted
- // by method name. Does not alter the order of identically named methods.
- // Property returns an array of all methods, including private methods. Caller is
- // responsible for stripping out any methods that are unwanted.
- */
- public MethodInfo[] OrderedMethods
- {
- virtual get
- {
- MethodInfo[] Methods = null;
-
- try
- {
- Methods = m_TypeObject.GetMethods( BindingFlags.LookupAll );
-
- // Order the member categories
- MethodComparer ICompMet = new MethodComparer();
- Array.Sort( Methods, ICompMet );
- }
- catch ( Exception Ex )
- {
- throw new Exception( "Occurred in LangType.OrderedMethods.", Ex );
- }
- return Methods;
- }
- }
-
- /*
- // OrderedProperties returns an array of PropertyInfos that are all the
- // PropertyInfos returned by the Type passed to this class's constructor, sorted
- // by property name. Does not alter the order of identically named properties.
- // Property returns an array of all properties, including nonpublic properties. Caller is
- // responsible for stripping out any properties that are unwanted.
- */
- public PropertyInfo[] OrderedProperties
- {
- virtual get
- {
- PropertyInfo[] Properties = null;
- try
- {
- Properties = m_TypeObject.GetProperties( BindingFlags.LookupAll );
- PropertyComparer ICompProp = new PropertyComparer();
- Array.Sort( Properties, ICompProp );
- }
- catch ( Exception Ex )
- {
- throw new Exception( "Occurred in LangType.OrderedProperties", Ex );
- }
- return Properties;
- }
- }
-
- /*
- // OrderedFields returns an array of FieldInfos that are all the
- // FieldInfos returned by the Type passed to this class's constructor, sorted
- // by field name. Does not alter the order of identically named fields.
- // Property returns an array of all fields, including nonpublic fields. Caller is
- // responsible for stripping out any fields that are unwanted.
- */
- public FieldInfo[] OrderedFields
- {
- virtual get
- {
- FieldInfo[] Fields = null;
- try
- {
- Fields = m_TypeObject.GetFields( BindingFlags.LookupAll );
- FieldComparer ICompField = new FieldComparer();
- Array.Sort( Fields, ICompField );
- }
- catch ( Exception Ex )
- {
- throw new Exception( "Occurred in LangType.OrderedFields.", Ex );
- }
- return Fields;
- }
- }
-
- /*
- // OrderedEvents returns an array of EventInfos that are all the
- // EventInfos returned by the Type passed to this class's constructor, sorted
- // by event name. Does not alter the order of identically named events.
- // Property returns an array of all events, including nonpublic events. Caller is
- // responsible for stripping out any events that are unwanted.
- */
- public EventInfo[] OrderedEvents
- {
- virtual get
- {
- EventInfo[] Events = null;
- try
- {
- Events = m_TypeObject.GetEvents( BindingFlags.LookupAll );
- EventComparer ICompEvent = new EventComparer();
- Array.Sort( Events, ICompEvent );
- }
- catch ( Exception Ex )
- {
- throw new Exception( "Occurred in LangType.OrderedEvents.", Ex );
- }
- return Events;
- }
- }
- }
-
- } // namespace LangUtil
-