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