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;
49 CodeTypeReferenceCollection typeArguments;
50 CodeTypeReferenceOptions referenceOptions;
58 public CodeTypeReference ()
64 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
66 public CodeTypeReference (string baseType)
72 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
74 public CodeTypeReference (Type baseType)
77 if (baseType == null) {
78 throw new ArgumentNullException ("baseType");
81 if (baseType.IsGenericParameter) {
82 this.baseType = baseType.Name;
83 this.referenceOptions = CodeTypeReferenceOptions.GenericTypeParameter;
85 else if (baseType.IsGenericTypeDefinition)
86 this.baseType = baseType.FullName;
87 else if (baseType.IsGenericType) {
88 this.baseType = baseType.GetGenericTypeDefinition ().FullName;
89 foreach (Type arg in baseType.GetGenericArguments ()) {
90 if (arg.IsGenericParameter)
91 TypeArguments.Add (new CodeTypeReference (new CodeTypeParameter (arg.Name)));
93 TypeArguments.Add (new CodeTypeReference (arg));
98 if (baseType.IsArray) {
99 this.arrayRank = baseType.GetArrayRank ();
100 this.arrayElementType = new CodeTypeReference (baseType.GetElementType ());
101 this.baseType = arrayElementType.BaseType;
103 Parse (baseType.FullName);
105 this.isInterface = baseType.IsInterface;
108 public CodeTypeReference( CodeTypeReference arrayElementType, int arrayRank )
110 this.baseType = null;
111 this.arrayRank = arrayRank;
112 this.arrayElementType = arrayElementType;
116 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
118 public CodeTypeReference( string baseType, int arrayRank )
119 : this (new CodeTypeReference (baseType), arrayRank)
124 public CodeTypeReference( CodeTypeParameter typeParameter ) :
125 this (typeParameter.Name)
127 this.referenceOptions = CodeTypeReferenceOptions.GenericTypeParameter;
130 public CodeTypeReference( string typeName, CodeTypeReferenceOptions referenceOptions ) :
133 this.referenceOptions = referenceOptions;
136 public CodeTypeReference( Type type, CodeTypeReferenceOptions referenceOptions ) :
139 this.referenceOptions = referenceOptions;
142 public CodeTypeReference( string typeName, params CodeTypeReference[] typeArguments ) :
145 TypeArguments.AddRange (typeArguments);
146 if (this.baseType.IndexOf ('`') < 0)
147 this.baseType += "`" + TypeArguments.Count;
155 public CodeTypeReference ArrayElementType
158 return arrayElementType;
161 arrayElementType = value;
165 public int ArrayRank {
174 public string BaseType {
176 if (arrayElementType != null && arrayRank > 0) {
177 return arrayElementType.BaseType;
180 if (baseType == null)
190 internal bool IsInterface {
191 get { return isInterface; }
194 private void Parse (string baseType)
196 if (baseType == null || baseType.Length == 0) {
197 this.baseType = typeof (void).FullName;
202 int array_start = baseType.IndexOf ('[');
203 if (array_start == -1) {
204 this.baseType = baseType;
208 int array_end = baseType.LastIndexOf (']');
209 if (array_end < array_start) {
210 this.baseType = baseType;
214 int lastAngle = baseType.LastIndexOf ('>');
215 if (lastAngle != -1 && lastAngle > array_end) {
216 this.baseType = baseType;
220 string[] args = baseType.Substring (array_start + 1, array_end - array_start - 1).Split (',');
222 if ((array_end - array_start) != args.Length) {
223 this.baseType = baseType.Substring (0, array_start);
225 int scanPos = array_start;
226 StringBuilder tb = new StringBuilder();
227 while (scanPos < baseType.Length) {
228 char currentChar = baseType[scanPos];
230 switch (currentChar) {
232 if (escapeCount > 1 && tb.Length > 0) {
233 tb.Append (currentChar);
239 if (escapeCount > 1 && tb.Length > 0) {
240 tb.Append (currentChar);
243 if (tb.Length != 0 && (escapeCount % 2) == 0) {
244 TypeArguments.Add (tb.ToString ());
249 if (escapeCount > 1) {
250 // skip anything after the type name until we
251 // reach the next separator
252 while (scanPos + 1 < baseType.Length) {
253 if (baseType[scanPos + 1] == ']') {
258 } else if (tb.Length > 0) {
259 CodeTypeReference typeArg = new CodeTypeReference (tb.ToString ());
260 TypeArguments.Add (typeArg);
265 tb.Append (currentChar);
271 arrayElementType = new CodeTypeReference (baseType.Substring (0, array_start));
272 arrayRank = args.Length;
275 int array_start = baseType.LastIndexOf ('[');
276 if (array_start == -1) {
277 this.baseType = baseType;
281 int array_end = baseType.LastIndexOf (']');
282 if (array_end < array_start) {
283 this.baseType = baseType;
287 string[] args = baseType.Substring (array_start + 1, array_end - array_start - 1).Split (',');
290 foreach (string arg in args) {
291 if (arg.Length != 0) {
297 arrayElementType = new CodeTypeReference (baseType.Substring (0, array_start));
298 arrayRank = args.Length;
300 this.baseType = baseType;
307 public CodeTypeReferenceOptions Options {
309 return referenceOptions;
312 referenceOptions = value;
317 public CodeTypeReferenceCollection TypeArguments {
319 if (typeArguments == null)
320 typeArguments = new CodeTypeReferenceCollection ();
321 return typeArguments;