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                 Type 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 Type[] GetTypeArgs ()
65                 {
66                         if (!instantiation.IsGenericType || instantiation.IsGenericParameter)
67                                 return null;                            
68
69                         return instantiation.GetGenericArguments ();
70                 }
71
72                 internal bool IsCompilerContext {
73                         get { return ((ModuleBuilder)base_method.Module).assemblyb.IsCompilerContext; }
74                 }
75
76                 //
77                 // MemberInfo members
78                 //
79                 
80                 public override Type DeclaringType {
81                         get {
82                                 return instantiation;
83                         }
84                 }
85
86                 public override string Name {
87                         get {
88                                 return base_method.Name;
89                         }
90                 }
91
92                 public override Type ReflectedType {
93                         get {
94                                 return instantiation;
95                         }
96                 }
97
98                 public override Type ReturnType {
99                         get { 
100                                 if (!IsCompilerContext)
101                                         return base_method.ReturnType;
102                                 return MonoGenericClass.InflateType (base_method.ReturnType, GetTypeArgs (), method_arguments);
103                         }
104                 }
105
106                 public override bool IsDefined (Type attributeType, bool inherit)
107                 {
108                         throw new NotSupportedException ();
109                 }
110
111                 public override object [] GetCustomAttributes (bool inherit)
112                 {
113                         throw new NotSupportedException ();
114                 }
115
116                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
117                 {
118                         throw new NotSupportedException ();
119                 }
120
121                 public override string ToString ()
122                 {
123                          //IEnumerable`1 get_Item(TKey)
124                          StringBuilder sb = new StringBuilder (ReturnType.ToString ());
125                          sb.Append (" ");
126                          sb.Append (base_method.Name);
127                          sb.Append ("(");
128                          if (IsCompilerContext) {
129                                  ParameterInfo [] par = GetParameters ();
130                                  for (int i = 0; i < par.Length; ++i) {
131                                         if (i > 0)
132                                                 sb.Append (", ");
133                                         sb.Append (par [i].ParameterType);
134                                  }
135                         }
136                          sb.Append (")");
137                          return sb.ToString ();
138                 }
139                 //
140                 // MethodBase members
141                 //
142
143                 public override MethodImplAttributes GetMethodImplementationFlags ()
144                 {
145                         return base_method.GetMethodImplementationFlags ();
146                 }
147
148                 public override ParameterInfo [] GetParameters ()
149                 {
150                         if (!IsCompilerContext)
151                                 throw new NotSupportedException ();
152
153                         ParameterInfo [] res = new ParameterInfo [base_method.parameters.Length];
154                         for (int i = 0; i < base_method.parameters.Length; i++) {
155                                 Type type = MonoGenericClass.InflateType (base_method.parameters [i], GetTypeArgs (), method_arguments);
156                                 res [i] = new ParameterInfo (base_method.pinfo == null ? null : base_method.pinfo [i + 1], type, this, i + 1);
157                         }
158                         return res;
159                 }
160
161                 public override int MetadataToken {
162                         get {
163                                 if (!IsCompilerContext)
164                                         return base.MetadataToken;
165                                 return base_method.MetadataToken;
166                         }
167                 }
168
169                 internal override int GetParameterCount ()
170                 {
171                         return base_method.GetParameterCount ();
172                 }
173
174                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
175                 {
176                         throw new NotSupportedException ();
177                 }
178
179                 public override RuntimeMethodHandle MethodHandle {
180                         get {
181                                 throw new NotSupportedException ();
182                         }
183                 }
184
185                 public override MethodAttributes Attributes {
186                         get {
187                                 return base_method.Attributes;
188                         }
189                 }
190
191                 public override CallingConventions CallingConvention {
192                         get {
193                                 return base_method.CallingConvention;
194                         }
195                 }
196
197                 public override MethodInfo MakeGenericMethod (params Type [] typeArguments)
198                 {
199                         if (base_method.generic_params == null || method_arguments != null)
200                                 throw new NotSupportedException (); //FIXME is this the right exception?
201
202                         if (typeArguments == null)
203                                 throw new ArgumentNullException ("typeArguments");
204
205                         foreach (Type t in typeArguments) {
206                                 if (t == null)
207                                         throw new ArgumentNullException ("typeArguments");
208                         }
209
210                         if (base_method.generic_params.Length != typeArguments.Length)
211                                 throw new ArgumentException ("Invalid argument array length");
212
213                         return new MethodOnTypeBuilderInst (this, typeArguments);
214                 }
215
216                 public override Type [] GetGenericArguments ()
217                 {
218                         if (base_method.generic_params == null)
219                                 return null;
220                         Type[] source = method_arguments ?? base_method.generic_params;
221                         Type[] result = new Type [source.Length];
222                         source.CopyTo (result, 0);
223                         return result;
224                 }
225
226                 public override MethodInfo GetGenericMethodDefinition ()
227                 {
228                         return (MethodInfo)generic_method_definition ?? base_method;
229                 }
230
231                 public override bool ContainsGenericParameters {
232                         get {
233                                 if (base_method.generic_params == null)
234                                         throw new NotSupportedException ();
235                                 if (method_arguments == null)
236                                         return true;
237                                 foreach (Type t in method_arguments) {
238                                         if (t.ContainsGenericParameters)
239                                                 return true;
240                                 }
241                                 return false;
242                         }
243                 }
244
245                 public override bool IsGenericMethodDefinition {
246                         get {
247                                 return base_method.generic_params != null && method_arguments == null;
248                         }
249                 }
250
251                 public override bool IsGenericMethod {
252                         get {
253                                 return base_method.generic_params != null;
254                         }
255                 }
256
257                 //
258                 // MethodInfo members
259                 //
260
261                 public override MethodInfo GetBaseDefinition ()
262                 {
263                         throw new NotSupportedException ();
264                 }
265
266                 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
267                         get {
268                                 throw new NotSupportedException ();
269                         }
270                 }
271         }
272 }
273