CodeTypeReference.cs :  » Development » devAdvantage » AnticipatingMinds » Genesis » CodeDOM » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » devAdvantage 
devAdvantage » AnticipatingMinds » Genesis » CodeDOM » CodeTypeReference.cs
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;

namespace AnticipatingMinds.Genesis.CodeDOM{
  /// <summary>
  /// Represents a reference to a type.
  /// </summary>
  [Serializable]
  public class CodeTypeReference : CodeElement
  {
    #region Instance constructors
    /// <summary>
    /// Initializes a new instance of the CodeTypeReference class using the System.Void data type.
    /// </summary>
    public CodeTypeReference(): this(typeof(void)){}
    /// <summary>
    /// Initializes a new instance of the CodeTypeReference class using the specified data type.
    /// </summary>
    /// <param name="type">The data type to reference.</param>
    public CodeTypeReference(Type type)
    {
      if (type.IsArray) 
      {
        this.arrayRank = type.GetArrayRank();
        this.arrayElementType = new CodeTypeReference(type.GetElementType());
        this.baseType = null;
      } 
      else 
      {
        this.arrayRank = 0;
        this.arrayElementType = null;
        this.baseType = type.FullName;
      }
    }
    /// <summary>
    /// Initializes a new instance of the CodeTypeReference class using the specified data type name.
    /// </summary>
    /// <param name="typeName">The name of the type to reference. </param>
    public CodeTypeReference(string typeName)
    {
      TypeName = typeName;
    }
    #endregion Instance constructors
    #region Public instance properties
    /// <summary>
    /// Gets the name of the data type being referenced.
    /// </summary>
    public string TypeName
    {
      get
      {
        if(isPointer)
          return pointerType.TypeName;

        if (arrayRank > 0 && arrayElementType != null) 
        {
          return arrayElementType.TypeName;
        }
        return (baseType == null) ? string.Empty : baseType;
      }
      set
      {
        if (value == null || value.Length == 0) 
        {
          value = typeof(void).FullName;
        }

        if(value[value.Length-1] == '*')
        {
          this.baseType = null;
          this.isPointer = true;
          this.pointerType = new CodeTypeReference(value.Substring(0, value.Length-1));
        }
        else
        {
          // See if this ends with standard array tail. If is is not an exact match, we pass it through verbatim.
          //we need to track inner square brackts too
          //string a[b,c[0]] is really string[,] for our purposes
          //string a[b[0]][] is string[][]
          //so, the firsth thing that we do is stripping stuff in between last brackates.

          isArray = false;
          int lastArrayClose = -1;
          int lastArrayOpen = -1;

          lastArrayClose = value.LastIndexOf(']');
          lastArrayOpen = -1;
          if(lastArrayClose >=0)
          {
            int closebrackets = 0;
            int pos = lastArrayClose; 
            while(pos >= 0)
            {
              if(value[pos] == ']')
                closebrackets++;

              if(value[pos] == '[')
              {
                closebrackets--;
                if(closebrackets == 0)
                {
                  lastArrayOpen = pos;
                  break;
                }
              }

              pos--;
            }
          }

          int arrayRank = 0;
          isArray = lastArrayOpen >= 0 && lastArrayClose == (value.Length - 1) && lastArrayOpen < lastArrayClose;
          if (isArray) 
          {
            arrayRank = 1;
            for (int index = lastArrayOpen + 1; index < lastArrayClose; index++) 
            {
              if (value[index] == ',') 
              {
                arrayRank++;
              }
            }
          }
          

          if (isArray) 
          {
            this.baseType = null;
            this.arrayRank = arrayRank;
            this.arrayElementType = new CodeTypeReference(value.Substring(0, lastArrayOpen));
            this.isPointer = false;
            this.pointerType = null;
          }
          else 
          {
            this.baseType = value;
            this.arrayRank = 0;
            this.arrayElementType = null;
            this.isPointer = false;
            this.pointerType = null;
          }
        }
      }
    }

    /// <summary>
    /// Returns an array element type if type is an array.
    /// </summary>
    public CodeTypeReference ArrayElementType 
    {
      get 
      {
        return arrayElementType;
      }
      set 
      {
        arrayElementType = value;
      }
    }
    /// <summary>
    /// Returns a pointer element type if type is a pointer.
    /// </summary>
    public CodeTypeReference PointerType
    {
      get
      {
        return pointerType;
      }
      set
      {
        pointerType = value;
      }
    }
    /// <summary>
    /// Returns true if it is a reference to an array type.
    /// </summary>
    public bool    IsArray
    {
      get
      {
        return isArray;
      }
    }

    /// <summary>
    /// Returns true if it is a reference to a pointer type.
    /// </summary>
    public bool    IsPointer
    {
      get
      {
        return isPointer;
      }
    }
    
    /// <summary>
    /// Gets an array rank if it is reference to an array type.
    /// </summary>
    public int    ArrayRank 
    {
      get 
      {
        return arrayRank;
      }
      set 
      {
        arrayRank = value;
      }
    }


    /// <summary>
    /// Gets code metadata type information if available.
    /// </summary>
    public CodeTypeInfo TypeInfo
    {
      get
      {
        return typeInfo;
      }
      set
      {
        typeInfo = value;
      }
    }

    #endregion Public instance properties
    #region Private instance fields
    private string baseType;
    private int arrayRank;
    private bool isArray = false;
    private bool isPointer = false;
    private CodeTypeReference arrayElementType;
    private CodeTypeReference pointerType;
    private CodeTypeInfo typeInfo;
    #endregion Private instance fields
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.