2 // System.CodeDom CodeTypeReferenceExpression Class implementation
5 // Daniel Stodden (stodden@in.tum.de)
6 // Marek Safar (marek.safar@seznam.cz)
8 // (C) 2001 Ximian, Inc.
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
35 namespace System.CodeDom
38 [ClassInterface(ClassInterfaceType.AutoDispatch)]
40 public class CodeTypeReference : CodeObject
42 private string baseType;
43 private CodeTypeReference arrayElementType;
44 private int arrayRank;
45 private bool isInterface;
48 CodeTypeReferenceCollection typeArguments;
49 CodeTypeReferenceOptions referenceOptions;
55 public CodeTypeReference ()
59 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
60 public CodeTypeReference (string baseType)
65 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
66 public CodeTypeReference (Type baseType)
68 if (baseType == null) {
69 throw new ArgumentNullException ("baseType");
72 if (baseType.IsGenericParameter) {
73 this.baseType = baseType.Name;
74 this.referenceOptions = CodeTypeReferenceOptions.GenericTypeParameter;
76 else if (baseType.IsGenericTypeDefinition)
77 this.baseType = baseType.FullName;
78 else if (baseType.IsGenericType) {
79 this.baseType = baseType.GetGenericTypeDefinition ().FullName;
80 foreach (Type arg in baseType.GetGenericArguments ()) {
81 if (arg.IsGenericParameter)
82 TypeArguments.Add (new CodeTypeReference (new CodeTypeParameter (arg.Name)));
84 TypeArguments.Add (new CodeTypeReference (arg));
87 else if (baseType.IsArray) {
88 this.arrayRank = baseType.GetArrayRank ();
89 this.arrayElementType = new CodeTypeReference (baseType.GetElementType ());
90 this.baseType = arrayElementType.BaseType;
92 Parse (baseType.FullName);
94 this.isInterface = baseType.IsInterface;
97 public CodeTypeReference( CodeTypeReference arrayElementType, int arrayRank )
100 this.arrayRank = arrayRank;
101 this.arrayElementType = arrayElementType;
104 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
105 public CodeTypeReference( string baseType, int arrayRank )
106 : this (new CodeTypeReference (baseType), arrayRank)
110 public CodeTypeReference( CodeTypeParameter typeParameter ) :
111 this (typeParameter.Name)
113 this.referenceOptions = CodeTypeReferenceOptions.GenericTypeParameter;
116 public CodeTypeReference( string typeName, CodeTypeReferenceOptions referenceOptions ) :
119 this.referenceOptions = referenceOptions;
122 public CodeTypeReference( Type type, CodeTypeReferenceOptions referenceOptions ) :
125 this.referenceOptions = referenceOptions;
128 public CodeTypeReference( string typeName, params CodeTypeReference[] typeArguments ) :
131 TypeArguments.AddRange (typeArguments);
132 if (this.baseType.IndexOf ('`') < 0)
133 this.baseType += "`" + TypeArguments.Count;
140 public CodeTypeReference ArrayElementType
143 return arrayElementType;
146 arrayElementType = value;
150 public int ArrayRank {
159 public string BaseType {
161 if (arrayElementType != null && arrayRank > 0) {
162 return arrayElementType.BaseType;
165 if (baseType == null)
175 internal bool IsInterface {
176 get { return isInterface; }
179 private void Parse (string baseType)
181 if (baseType == null || baseType.Length == 0) {
182 this.baseType = typeof (void).FullName;
186 int array_start = baseType.IndexOf ('[');
187 if (array_start == -1) {
188 this.baseType = baseType;
192 int array_end = baseType.LastIndexOf (']');
193 if (array_end < array_start) {
194 this.baseType = baseType;
198 int lastAngle = baseType.LastIndexOf ('>');
199 if (lastAngle != -1 && lastAngle > array_end) {
200 this.baseType = baseType;
204 string[] args = baseType.Substring (array_start + 1, array_end - array_start - 1).Split (',');
206 if ((array_end - array_start) != args.Length) {
207 this.baseType = baseType.Substring (0, array_start);
209 int scanPos = array_start;
210 StringBuilder tb = new StringBuilder();
211 while (scanPos < baseType.Length) {
212 char currentChar = baseType[scanPos];
214 switch (currentChar) {
216 if (escapeCount > 1 && tb.Length > 0) {
217 tb.Append (currentChar);
223 if (escapeCount > 1 && tb.Length > 0) {
224 tb.Append (currentChar);
227 if (tb.Length != 0 && (escapeCount % 2) == 0) {
228 TypeArguments.Add (tb.ToString ());
233 if (escapeCount > 1) {
234 // skip anything after the type name until we
235 // reach the next separator
236 while (scanPos + 1 < baseType.Length) {
237 if (baseType[scanPos + 1] == ']') {
242 } else if (tb.Length > 0) {
243 CodeTypeReference typeArg = new CodeTypeReference (tb.ToString ());
244 TypeArguments.Add (typeArg);
249 tb.Append (currentChar);
255 arrayElementType = new CodeTypeReference (baseType.Substring (0, array_start));
256 arrayRank = args.Length;
261 public CodeTypeReferenceOptions Options {
263 return referenceOptions;
266 referenceOptions = value;
271 public CodeTypeReferenceCollection TypeArguments {
273 if (typeArguments == null)
274 typeArguments = new CodeTypeReferenceCollection ();
275 return typeArguments;