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 arrayType;
45 private bool isInterface;
48 CodeTypeReferenceCollection typeArguments;
49 CodeTypeReferenceOptions codeTypeReferenceOption;
57 public CodeTypeReference ()
63 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
65 public CodeTypeReference (string baseType)
71 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
73 public CodeTypeReference (Type baseType)
76 if (baseType == null) {
77 throw new ArgumentNullException ("baseType");
80 if (baseType.IsArray) {
81 this.rank = baseType.GetArrayRank ();
82 this.arrayType = new CodeTypeReference (baseType.GetElementType ());
83 this.baseType = arrayType.BaseType;
85 Parse (baseType.FullName);
87 this.isInterface = baseType.IsInterface;
90 public CodeTypeReference( CodeTypeReference arrayType, int rank )
94 this.arrayType = arrayType;
98 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
100 public CodeTypeReference( string baseType, int rank )
101 : this (new CodeTypeReference (baseType), rank)
106 public CodeTypeReference( CodeTypeParameter typeParameter ) :
107 this (typeParameter.Name)
109 this.codeTypeReferenceOption = CodeTypeReferenceOptions.GenericTypeParameter;
112 public CodeTypeReference( string typeName, CodeTypeReferenceOptions codeTypeReferenceOption ) :
115 this.codeTypeReferenceOption = codeTypeReferenceOption;
118 public CodeTypeReference( Type type, CodeTypeReferenceOptions codeTypeReferenceOption ) :
121 this.codeTypeReferenceOption = codeTypeReferenceOption;
124 public CodeTypeReference( string typeName, params CodeTypeReference[] typeArguments ) :
127 TypeArguments.AddRange (typeArguments);
135 public CodeTypeReference ArrayElementType
145 public int ArrayRank {
154 public string BaseType {
156 if (arrayType != null && rank > 0) {
157 return arrayType.BaseType;
160 if (baseType == null)
170 internal bool IsInterface {
171 get { return isInterface; }
174 private void Parse (string baseType)
176 if (baseType == null || baseType.Length == 0) {
177 this.baseType = typeof (void).FullName;
182 int array_start = baseType.IndexOf ('[');
183 if (array_start == -1) {
184 this.baseType = baseType;
188 int array_end = baseType.LastIndexOf (']');
189 if (array_end < array_start) {
190 this.baseType = baseType;
194 string[] args = baseType.Substring (array_start + 1, array_end - array_start - 1).Split (',');
196 if ((array_end - array_start) != args.Length) {
197 this.baseType = baseType.Substring (0, array_start);
199 int scanPos = array_start;
200 StringBuilder tb = new StringBuilder();
201 while (scanPos < baseType.Length) {
202 char currentChar = baseType[scanPos];
204 switch (currentChar) {
206 if (escapeCount > 1 && tb.Length > 0) {
207 tb.Append (currentChar);
213 if (escapeCount > 1 && tb.Length > 0) {
214 tb.Append (currentChar);
217 if (tb.Length != 0 && (escapeCount % 2) == 0) {
218 TypeArguments.Add (tb.ToString ());
223 if (escapeCount > 1) {
224 // skip anything after the type name until we
225 // reach the next separator
226 while (scanPos + 1 < baseType.Length) {
227 if (baseType[scanPos + 1] == ']') {
232 } else if (tb.Length > 0) {
233 CodeTypeReference typeArg = new CodeTypeReference (tb.ToString ());
234 TypeArguments.Add (typeArg);
239 tb.Append (currentChar);
245 arrayType = new CodeTypeReference (baseType.Substring (0, array_start));
249 int array_start = baseType.LastIndexOf ('[');
250 if (array_start == -1) {
251 this.baseType = baseType;
255 int array_end = baseType.LastIndexOf (']');
256 if (array_end < array_start) {
257 this.baseType = baseType;
261 string[] args = baseType.Substring (array_start + 1, array_end - array_start - 1).Split (',');
264 foreach (string arg in args) {
265 if (arg.Length != 0) {
271 arrayType = new CodeTypeReference (baseType.Substring (0, array_start));
274 this.baseType = baseType;
281 public CodeTypeReferenceOptions Options {
283 return codeTypeReferenceOption;
286 codeTypeReferenceOption = value;
291 public CodeTypeReferenceCollection TypeArguments {
293 if (typeArguments == null)
294 typeArguments = new CodeTypeReferenceCollection ();
295 return typeArguments;