Show thumbnail images in SWF.(Open,Save)FileDialog
[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 #if !FULL_AOT_RUNTIME
31 using System;
32 using System.Globalization;
33 using System.Reflection;
34 using System.Text;
35 using System.Runtime.InteropServices;
36
37
38 namespace System.Reflection.Emit
39 {
40         /*
41          * This class represents a method of an instantiation of a generic type builder.
42          */
43         [StructLayout (LayoutKind.Sequential)]
44         internal class MethodOnTypeBuilderInst : MethodInfo
45         {
46                 #region Keep in sync with object-internals.h
47                 Type instantiation;
48                 MethodInfo base_method; /*This is the base method definition, it must be non-inflated and belong to a non-inflated type.*/
49                 Type[] method_arguments;
50                 #endregion
51                 MethodInfo generic_method_definition;
52
53                 public MethodOnTypeBuilderInst (MonoGenericClass instantiation, MethodInfo base_method)
54                 {
55                         this.instantiation = instantiation;
56                         this.base_method = base_method;
57                 }
58
59                 internal MethodOnTypeBuilderInst (MethodOnTypeBuilderInst gmd, Type[] typeArguments)
60                 {
61                         this.instantiation = gmd.instantiation;
62                         this.base_method = gmd.base_method;
63                         this.method_arguments = new Type [typeArguments.Length];
64                         typeArguments.CopyTo (this.method_arguments, 0);
65                         this.generic_method_definition = gmd;
66                 }
67
68                 internal MethodOnTypeBuilderInst (MethodInfo method, Type[] typeArguments)
69                 {
70                         this.instantiation = method.DeclaringType;
71                         this.base_method = ExtractBaseMethod (method);
72                         this.method_arguments = new Type [typeArguments.Length];
73                         typeArguments.CopyTo (this.method_arguments, 0);
74                         if (base_method != method)
75                                 this.generic_method_definition = method;
76                 }
77
78                 static MethodInfo ExtractBaseMethod (MethodInfo info)
79                 {
80                         if (info is MethodBuilder)
81                                 return info;
82                         if (info is MethodOnTypeBuilderInst)
83                                 return ((MethodOnTypeBuilderInst)info).base_method;
84
85                         if (info.IsGenericMethod)
86                                 info = info.GetGenericMethodDefinition ();
87
88                         Type t = info.DeclaringType;
89                         if (!t.IsGenericType || t.IsGenericTypeDefinition)
90                                 return info;
91
92                         return (MethodInfo)t.Module.ResolveMethod (info.MetadataToken);
93                 }
94
95                 internal Type[] GetTypeArgs ()
96                 {
97                         if (!instantiation.IsGenericType || instantiation.IsGenericParameter)
98                                 return null;                            
99
100                         return instantiation.GetGenericArguments ();
101                 }
102
103                 //
104                 // MemberInfo members
105                 //
106                 
107                 public override Type DeclaringType {
108                         get {
109                                 return instantiation;
110                         }
111                 }
112
113                 public override string Name {
114                         get {
115                                 return base_method.Name;
116                         }
117                 }
118
119                 public override Type ReflectedType {
120                         get {
121                                 return instantiation;
122                         }
123                 }
124
125                 public override Type ReturnType {
126                         get { 
127                                 return base_method.ReturnType;
128                         }
129                 }
130
131                 public override bool IsDefined (Type attributeType, bool inherit)
132                 {
133                         throw new NotSupportedException ();
134                 }
135
136                 public override object [] GetCustomAttributes (bool inherit)
137                 {
138                         throw new NotSupportedException ();
139                 }
140
141                 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
142                 {
143                         throw new NotSupportedException ();
144                 }
145
146                 public override string ToString ()
147                 {
148                          //IEnumerable`1 get_Item(TKey)
149                          StringBuilder sb = new StringBuilder (ReturnType.ToString ());
150                          sb.Append (" ");
151                          sb.Append (base_method.Name);
152                          sb.Append ("(");
153                          sb.Append (")");
154                          return sb.ToString ();
155                 }
156                 //
157                 // MethodBase members
158                 //
159
160                 public override MethodImplAttributes GetMethodImplementationFlags ()
161                 {
162                         return base_method.GetMethodImplementationFlags ();
163                 }
164
165                 public override ParameterInfo [] GetParameters ()
166                 {
167                         return GetParametersInternal ();
168                 }
169
170                 internal override ParameterInfo [] GetParametersInternal ()
171                 {
172                         throw new NotSupportedException ();
173                 }
174
175                 public override int MetadataToken {
176                         get {
177                                 return base.MetadataToken;
178                         }
179                 }
180
181                 internal override int GetParametersCount ()
182                 {
183                         return base_method.GetParametersCount ();
184                 }
185
186                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
187                 {
188                         throw new NotSupportedException ();
189                 }
190
191                 public override RuntimeMethodHandle MethodHandle {
192                         get {
193                                 throw new NotSupportedException ();
194                         }
195                 }
196
197                 public override MethodAttributes Attributes {
198                         get {
199                                 return base_method.Attributes;
200                         }
201                 }
202
203                 public override CallingConventions CallingConvention {
204                         get {
205                                 return base_method.CallingConvention;
206                         }
207                 }
208
209                 public override MethodInfo MakeGenericMethod (params Type [] methodInstantiation)
210                 {
211                         if (!base_method.IsGenericMethodDefinition || (method_arguments != null))
212                                 throw new InvalidOperationException ("Method is not a generic method definition");
213
214                         if (methodInstantiation == null)
215                                 throw new ArgumentNullException ("methodInstantiation");
216
217                         if (base_method.GetGenericArguments ().Length != methodInstantiation.Length)
218                                 throw new ArgumentException ("Incorrect length", "methodInstantiation");
219
220                         foreach (Type type in methodInstantiation) {
221                                 if (type == null)
222                                         throw new ArgumentNullException ("methodInstantiation");
223                         }
224
225                         return new MethodOnTypeBuilderInst (this, methodInstantiation);
226                 }
227
228                 public override Type [] GetGenericArguments ()
229                 {
230                         if (!base_method.IsGenericMethodDefinition)
231                                 return null;
232                         Type[] source = method_arguments ?? base_method.GetGenericArguments ();
233                         Type[] result = new Type [source.Length];
234                         source.CopyTo (result, 0);
235                         return result;
236                 }
237
238                 public override MethodInfo GetGenericMethodDefinition ()
239                 {
240                         return generic_method_definition ?? base_method;
241                 }
242
243                 public override bool ContainsGenericParameters {
244                         get {
245                                 if (base_method.ContainsGenericParameters)
246                                         return true;
247                                 if (!base_method.IsGenericMethodDefinition)
248                                         throw new NotSupportedException ();
249                                 if (method_arguments == null)
250                                         return true;
251                                 foreach (Type t in method_arguments) {
252                                         if (t.ContainsGenericParameters)
253                                                 return true;
254                                 }
255                                 return false;
256                         }
257                 }
258
259                 public override bool IsGenericMethodDefinition {
260                         get {
261                                 return base_method.IsGenericMethodDefinition && method_arguments == null;
262                         }
263                 }
264
265                 public override bool IsGenericMethod {
266                         get {
267                                 return base_method.IsGenericMethodDefinition;
268                         }
269                 }
270
271                 //
272                 // MethodInfo members
273                 //
274
275                 public override MethodInfo GetBaseDefinition ()
276                 {
277                         throw new NotSupportedException ();
278                 }
279
280                 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
281                         get {
282                                 throw new NotSupportedException ();
283                         }
284                 }
285         }
286 }
287
288 #endif