svn path=/branches/mono-1-1-9/mcs/; revision=51212
[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;\r
45                 private bool isInterface;
46
47 #if NET_2_0
48                 CodeTypeReferenceCollection typeArguments;
49                 CodeTypeReferenceOptions codeTypeReferenceOption;
50 #endif
51
52                 //
53                 // Constructors
54                 //
55                 public CodeTypeReference( string baseType )
56                 {
57                         if (baseType == null || baseType.Length == 0) {
58                                 this.baseType = typeof (void).FullName;
59                                 return;
60                         }
61
62                         int array_start = baseType.LastIndexOf ('[');
63                         if (array_start == -1) {
64                                 this.baseType = baseType;
65                                 return;
66                         }
67                         string[] args = baseType.Split (',');
68
69 #if NET_2_0
70                         int array_end = baseType.LastIndexOf (']');
71
72                         if ((array_end - array_start) != args.Length) {
73                                 arrayType = new CodeTypeReference (baseType.Substring (0, array_start));
74                                 array_start++;
75                                 TypeArguments.Add (new CodeTypeReference (baseType.Substring (array_start, array_end - array_start)));
76                         } else
77 #endif
78                                 arrayType = new CodeTypeReference (baseType.Substring (0, array_start), args.Length);
79                 }
80                 
81                 public CodeTypeReference( Type baseType )
82                 {
83 #if NET_2_0
84                         if (baseType == null) {
85                                 throw new ArgumentNullException ("baseType");
86                         }
87 #endif\r
88                         if (baseType.IsArray) {\r
89                                 this.rank = baseType.GetArrayRank ();\r
90                                 this.arrayType = new CodeTypeReference (baseType.GetElementType ());\r
91                                 this.baseType = arrayType.BaseType;\r
92                         } else {\r
93                                 this.baseType = baseType.FullName;\r
94                         }\r
95                         this.isInterface = baseType.IsInterface;
96                 }
97
98                 public CodeTypeReference( CodeTypeReference arrayType, int rank )
99                 {
100                         this.baseType = null;
101                         this.rank = rank;
102                         this.arrayType = arrayType;
103                 }
104
105                 public CodeTypeReference( string baseType, int rank )
106                         : this (new CodeTypeReference (baseType), rank)
107                 {
108                 }
109                         
110 #if NET_2_0
111                 public CodeTypeReference( CodeTypeParameter typeParameter ) :
112                         this (typeParameter.Name)
113                 {
114                 }
115
116                 public CodeTypeReference( string typeName, CodeTypeReferenceOptions codeTypeReferenceOption ) :
117                         this (typeName)
118                 {
119                         this.codeTypeReferenceOption = codeTypeReferenceOption;
120                 }
121
122                 public CodeTypeReference( Type type, CodeTypeReferenceOptions codeTypeReferenceOption ) :
123                         this (type)
124                 {
125                         this.codeTypeReferenceOption = codeTypeReferenceOption;
126                 }
127
128                 public CodeTypeReference( string typeName, params CodeTypeReference[] typeArguments ) :
129                         this (typeName)
130                 {
131                         TypeArguments.AddRange (typeArguments);
132                 }
133 #endif
134
135                 //
136                 // Properties
137                 //
138
139                 public CodeTypeReference ArrayElementType
140                 {
141                         get {
142                                 return arrayType;
143                         }
144                         set {
145                                 arrayType = value;
146                         }
147                 }
148                 
149                 public int ArrayRank {
150                         get {
151                                 return rank;
152                         }
153                         set {
154                                 rank = value;
155                         }
156                 }
157
158                 public string BaseType {
159                         get {
160                                 if (baseType == null)
161                                         return String.Empty;
162
163                                 return baseType;
164                         }
165                         set {
166                                 baseType = value;
167                         }
168                 }\r
169 \r
170                 internal bool IsInterface {\r
171                         get { return isInterface; }\r
172                 }
173
174 #if NET_2_0
175                 [ComVisible (false)]
176                 public CodeTypeReferenceOptions Options {
177                         get {
178                                 return codeTypeReferenceOption;
179                         }
180                         set {
181                                 codeTypeReferenceOption = value;
182                         }
183                 }
184
185                 [ComVisible (false)]
186                 public CodeTypeReferenceCollection TypeArguments {
187                         get {
188                                 if (typeArguments == null)
189                                         typeArguments = new CodeTypeReferenceCollection ();
190                                 return typeArguments;
191                         }
192                 }
193 #endif
194         }
195 }