2009-12-04 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / MethodOnTypeBuilderInst.cs
1 //
2 // System.Reflection.Emit/MethodOnTypeBuilderInst.cs
3 //
4 // Author:
5 //   Zoltan Varga (vargaz@gmail.com)
6 //
7 //
8 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Globalization;
32 using System.Reflection;
33 using System.Text;
34
35 namespace System.Reflection.Emit
36 {
37         /*
38          * This class represents a method of an instantiation of a generic type builder.
39          */
40         internal class MethodOnTypeBuilderInst : MethodInfo
41         {
42                 #region Keep in sync with object-internals.h
43                 MonoGenericClass instantiation;
44                 internal MethodBuilder base_method; /*This is the base method definition, it must be non-inflated and belong to a non-inflated type.*/
45                 Type[] method_arguments;
46                 MethodOnTypeBuilderInst generic_method_definition;
47                 #endregion
48
49                 public MethodOnTypeBuilderInst (MonoGenericClass instantiation, MethodBuilder base_method)
50                 {
51                         this.instantiation = instantiation;
52                         this.base_method = base_method;
53                 }
54
55                 internal MethodOnTypeBuilderInst (MethodOnTypeBuilderInst gmd, Type[] typeArguments)
56                 {
57                         this.instantiation = gmd.instantiation;
58                         this.base_method = gmd.base_method;
59                         this.method_arguments = new Type [typeArguments.Length];
60                         typeArguments.CopyTo (this.method_arguments, 0);
61                         this.generic_method_definition = gmd;
62                 }
63
64                 internal bool IsCompilerContext {
65                         get { return ((ModuleBuilder)base_method.Module).assemblyb.IsCompilerContext; }
66                 }
67
68                 //
69                 // MemberInfo members
70                 //
71                 
72                 public override Type DeclaringType {
73                         get {
74                                 return instantiation;
75                         }
76                 }
77
78                 public override string Name {
79                         get {
80                                 return base_method.Name;
81                         }
82                 }
83
84                 public override Type ReflectedType {
85                         get {
86                                 return instantiation;
87                         }
88                 }
89
90                 public override Type ReturnType {
91                         get { 
92                                 if (!IsCompilerContext)
93                                         return base_method.ReturnType;
94                                 return instantiation.InflateType (base_method.ReturnType, method_arguments);
95                         }
96                 }
97
98                 public override bool IsDefined (Type attributeType, bool inherit)
99                 {
100                         throw new NotSupportedException ();
101                 }
102
103                 public override object [] GetCustomAttributes (bool inherit)
104                 {
105                         throw new NotSupportedException ();
106                 }
107
108                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
109                 {
110                         throw new NotSupportedException ();
111                 }
112
113                 public override string ToString ()
114                 {
115                          //IEnumerable`1 get_Item(TKey)
116                          StringBuilder sb = new StringBuilder (ReturnType.ToString ());
117                          sb.Append (" ");
118                          sb.Append (base_method.Name);
119                          sb.Append ("(");
120                          if (IsCompilerContext) {
121                                  ParameterInfo [] par = GetParameters ();
122                                  for (int i = 0; i < par.Length; ++i) {
123                                         if (i > 0)
124                                                 sb.Append (", ");
125                                         sb.Append (par [i].ParameterType);
126                                  }
127                         }
128                          sb.Append (")");
129                          return sb.ToString ();
130                 }
131                 //
132                 // MethodBase members
133                 //
134
135                 public override MethodImplAttributes GetMethodImplementationFlags ()
136                 {
137                         return base_method.GetMethodImplementationFlags ();
138                 }
139
140                 public override ParameterInfo [] GetParameters ()
141                 {
142                         if (!IsCompilerContext)
143                                 throw new NotSupportedException ();
144
145                         ParameterInfo [] res = new ParameterInfo [base_method.parameters.Length];
146                         for (int i = 0; i < base_method.parameters.Length; i++) {
147                                 Type type = instantiation.InflateType (base_method.parameters [i], method_arguments);
148                                 res [i] = new ParameterInfo (base_method.pinfo == null ? null : base_method.pinfo [i + 1], type, this, i + 1);
149                         }
150                         return res;
151                 }
152
153                 public override int MetadataToken {
154                         get {
155                                 if (!IsCompilerContext)
156                                         return base.MetadataToken;
157                                 return base_method.MetadataToken;
158                         }
159                 }
160
161                 internal override int GetParameterCount ()
162                 {
163                         return base_method.GetParameterCount ();
164                 }
165
166                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
167                 {
168                         throw new NotSupportedException ();
169                 }
170
171                 public override RuntimeMethodHandle MethodHandle {
172                         get {
173                                 throw new NotSupportedException ();
174                         }
175                 }
176
177                 public override MethodAttributes Attributes {
178                         get {
179                                 return base_method.Attributes;
180                         }
181                 }
182
183                 public override CallingConventions CallingConvention {
184                         get {
185                                 return base_method.CallingConvention;
186                         }
187                 }
188
189                 public override MethodInfo MakeGenericMethod (params Type [] typeArguments)
190                 {
191                         if (base_method.generic_params == null || method_arguments != null)
192                                 throw new NotSupportedException (); //FIXME is this the right exception?
193
194                         if (typeArguments == null)
195                                 throw new ArgumentNullException ("typeArguments");
196
197                         foreach (Type t in typeArguments) {
198                                 if (t == null)
199                                         throw new ArgumentNullException ("typeArguments");
200                         }
201
202                         if (base_method.generic_params.Length != typeArguments.Length)
203                                 throw new ArgumentException ("Invalid argument array length");
204
205                         return new MethodOnTypeBuilderInst (this, typeArguments);
206                 }
207
208                 public override Type [] GetGenericArguments ()
209                 {
210                         if (base_method.generic_params == null)
211                                 return null;
212                         Type[] source = method_arguments ?? base_method.generic_params;
213                         Type[] result = new Type [source.Length];
214                         source.CopyTo (result, 0);
215                         return result;
216                 }
217
218                 public override MethodInfo GetGenericMethodDefinition ()
219                 {
220                         return (MethodInfo)generic_method_definition ?? base_method;
221                 }
222
223                 public override bool ContainsGenericParameters {
224                         get {
225                                 if (base_method.generic_params == null)
226                                         throw new NotSupportedException ();
227                                 if (method_arguments == null)
228                                         return true;
229                                 foreach (Type t in method_arguments) {
230                                         if (t.ContainsGenericParameters)
231                                                 return true;
232                                 }
233                                 return false;
234                         }
235                 }
236
237                 public override bool IsGenericMethodDefinition {
238                         get {
239                                 return base_method.generic_params != null && method_arguments == null;
240                         }
241                 }
242
243                 public override bool IsGenericMethod {
244                         get {
245                                 return base_method.generic_params != null;
246                         }
247                 }
248
249                 //
250                 // MethodInfo members
251                 //
252
253                 public override MethodInfo GetBaseDefinition ()
254                 {
255                         throw new NotSupportedException ();
256                 }
257
258                 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
259                         get {
260                                 throw new NotSupportedException ();
261                         }
262                 }
263         }
264 }
265