* CodeTypeReferenceTest.cs: Added tests for CodeTypeParameter ctor,
[mono.git] / mcs / class / System / System.CodeDom / CodeTypeReference.cs
1 //
2 // System.CodeDom CodeTypeReferenceExpression Class implementation
3 //
4 // Author:
5 //   Daniel Stodden (stodden@in.tum.de)
6 //   Marek Safar (marek.safar@seznam.cz)
7 //
8 // (C) 2001 Ximian, Inc.
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System.Runtime.InteropServices;
33
34 namespace System.CodeDom
35 {
36         [Serializable]
37         [ClassInterface(ClassInterfaceType.AutoDispatch)]
38         [ComVisible(true)]
39         public class CodeTypeReference
40                 : CodeObject
41         {
42                 private string baseType;
43                 private CodeTypeReference arrayType;
44                 private int rank;
45                 private bool isInterface;
46
47 #if NET_2_0
48                 CodeTypeReferenceCollection typeArguments;
49                 CodeTypeReferenceOptions codeTypeReferenceOption;
50 #endif
51
52                 //
53                 // Constructors
54                 //
55
56 #if NET_2_0
57                 public CodeTypeReference ()
58                 {
59                 }
60 #endif
61
62                 public CodeTypeReference (string baseType)
63                 {
64                         if (baseType == null || baseType.Length == 0) {
65                                 this.baseType = typeof (void).FullName;
66                                 return;
67                         }
68
69                         int array_start = baseType.LastIndexOf ('[');
70                         if (array_start == -1) {
71                                 this.baseType = baseType;
72                                 return;
73                         }
74
75                         int array_end = baseType.LastIndexOf (']');
76                         if (array_end < array_start) {
77                                 this.baseType = baseType;
78                                 return;
79                         }
80
81                         string[] args = baseType.Substring (array_start + 1, array_end - array_start - 1).Split (',');
82
83 #if NET_2_0
84                         if ((array_end - array_start) != args.Length) {
85                                 this.baseType = baseType.Substring(0, array_start);
86                                 foreach (string arg in args) {
87                                         if (arg.Length != 0) {
88                                                 TypeArguments.Add (new CodeTypeReference (arg));
89                                         }
90                                 }
91                         } else {
92                                 arrayType = new CodeTypeReference (baseType.Substring (0, array_start));
93                                 rank = args.Length;
94                         }
95 #else
96                         bool isArray = true;
97                         foreach (string arg in args) {
98                                 if (arg.Length != 0) {
99                                         isArray = false;
100                                         break;
101                                 }
102                         }
103                         if (isArray) {
104                                 arrayType = new CodeTypeReference (baseType.Substring (0, array_start));
105                                 rank = args.Length;
106                         } else {
107                                 this.baseType = baseType;
108                         }
109 #endif
110                 }
111                 
112                 public CodeTypeReference( Type baseType )
113                 {
114 #if NET_2_0
115                         if (baseType == null) {
116                                 throw new ArgumentNullException ("baseType");
117                         }
118 #endif
119                         if (baseType.IsArray) {
120                                 this.rank = baseType.GetArrayRank ();
121                                 this.arrayType = new CodeTypeReference (baseType.GetElementType ());
122                                 this.baseType = arrayType.BaseType;
123                         } else {
124                                 this.baseType = baseType.FullName;
125                         }
126                         this.isInterface = baseType.IsInterface;
127                 }
128
129                 public CodeTypeReference( CodeTypeReference arrayType, int rank )
130                 {
131                         this.baseType = null;
132                         this.rank = rank;
133                         this.arrayType = arrayType;
134                 }
135
136                 public CodeTypeReference( string baseType, int rank )
137                         : this (new CodeTypeReference (baseType), rank)
138                 {
139                 }
140
141 #if NET_2_0
142                 public CodeTypeReference( CodeTypeParameter typeParameter ) :
143                         this (typeParameter.Name)
144                 {
145                         this.codeTypeReferenceOption = CodeTypeReferenceOptions.GenericTypeParameter;
146                 }
147
148                 public CodeTypeReference( string typeName, CodeTypeReferenceOptions codeTypeReferenceOption ) :
149                         this (typeName)
150                 {
151                         this.codeTypeReferenceOption = codeTypeReferenceOption;
152                 }
153
154                 public CodeTypeReference( Type type, CodeTypeReferenceOptions codeTypeReferenceOption ) :
155                         this (type)
156                 {
157                         this.codeTypeReferenceOption = codeTypeReferenceOption;
158                 }
159
160                 public CodeTypeReference( string typeName, params CodeTypeReference[] typeArguments ) :
161                         this (typeName)
162                 {
163                         TypeArguments.AddRange (typeArguments);
164                 }
165 #endif
166
167                 //
168                 // Properties
169                 //
170
171                 public CodeTypeReference ArrayElementType
172                 {
173                         get {
174                                 return arrayType;
175                         }
176                         set {
177                                 arrayType = value;
178                         }
179                 }
180                 
181                 public int ArrayRank {
182                         get {
183                                 return rank;
184                         }
185                         set {
186                                 rank = value;
187                         }
188                 }
189
190                 public string BaseType {
191                         get {
192                                 if (arrayType != null) {
193                                         return arrayType.BaseType;
194                                 }
195
196                                 if (baseType == null)
197                                         return String.Empty;
198
199                                 return baseType;
200                         }
201                         set {
202                                 baseType = value;
203                         }
204                 }
205
206                 internal bool IsInterface {
207                         get { return isInterface; }
208                 }
209
210 #if NET_2_0
211                 [ComVisible (false)]
212                 public CodeTypeReferenceOptions Options {
213                         get {
214                                 return codeTypeReferenceOption;
215                         }
216                         set {
217                                 codeTypeReferenceOption = value;
218                         }
219                 }
220
221                 [ComVisible (false)]
222                 public CodeTypeReferenceCollection TypeArguments {
223                         get {
224                                 if (typeArguments == null)
225                                         typeArguments = new CodeTypeReferenceCollection ();
226                                 return typeArguments;
227                         }
228                 }
229 #endif
230         }
231 }