2 // System.Reflection.Emit/MethodOnTypeBuilderInst.cs
5 // Zoltan Varga (vargaz@gmail.com)
8 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Globalization;
33 using System.Reflection;
35 using System.Runtime.InteropServices;
38 namespace System.Reflection.Emit
41 * This class represents a method of an instantiation of a generic type builder.
43 [StructLayout (LayoutKind.Sequential)]
44 internal class MethodOnTypeBuilderInst : MethodInfo
46 #region Keep in sync with object-internals.h
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;
51 MethodInfo generic_method_definition;
53 public MethodOnTypeBuilderInst (MonoGenericClass instantiation, MethodInfo base_method)
55 this.instantiation = instantiation;
56 this.base_method = base_method;
59 internal MethodOnTypeBuilderInst (MethodOnTypeBuilderInst gmd, Type[] typeArguments)
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;
68 internal MethodOnTypeBuilderInst (MethodInfo method, Type[] typeArguments)
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;
78 static MethodInfo ExtractBaseMethod (MethodInfo info)
80 if (info is MethodBuilder)
82 if (info is MethodOnTypeBuilderInst)
83 return ((MethodOnTypeBuilderInst)info).base_method;
85 if (info.IsGenericMethod)
86 info = info.GetGenericMethodDefinition ();
88 Type t = info.DeclaringType;
89 if (!t.IsGenericType || t.IsGenericTypeDefinition)
92 return (MethodInfo)t.Module.ResolveMethod (info.MetadataToken);
95 internal Type[] GetTypeArgs ()
97 if (!instantiation.IsGenericType || instantiation.IsGenericParameter)
100 return instantiation.GetGenericArguments ();
104 // MemberInfo members
107 public override Type DeclaringType {
109 return instantiation;
113 public override string Name {
115 return base_method.Name;
119 public override Type ReflectedType {
121 return instantiation;
125 public override Type ReturnType {
127 return base_method.ReturnType;
131 public override Module Module {
133 return base_method.Module;
137 public override bool IsDefined (Type attributeType, bool inherit)
139 throw new NotSupportedException ();
142 public override object [] GetCustomAttributes (bool inherit)
144 throw new NotSupportedException ();
147 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
149 throw new NotSupportedException ();
152 public override string ToString ()
154 //IEnumerable`1 get_Item(TKey)
155 StringBuilder sb = new StringBuilder (ReturnType.ToString ());
157 sb.Append (base_method.Name);
160 return sb.ToString ();
163 // MethodBase members
166 public override MethodImplAttributes GetMethodImplementationFlags ()
168 return base_method.GetMethodImplementationFlags ();
171 public override ParameterInfo [] GetParameters ()
173 return GetParametersInternal ();
176 internal override ParameterInfo [] GetParametersInternal ()
178 throw new NotSupportedException ();
181 public override int MetadataToken {
183 return base.MetadataToken;
187 internal override int GetParametersCount ()
189 return base_method.GetParametersCount ();
192 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
194 throw new NotSupportedException ();
197 public override RuntimeMethodHandle MethodHandle {
199 throw new NotSupportedException ();
203 public override MethodAttributes Attributes {
205 return base_method.Attributes;
209 public override CallingConventions CallingConvention {
211 return base_method.CallingConvention;
215 public override MethodInfo MakeGenericMethod (params Type [] methodInstantiation)
217 if (!base_method.IsGenericMethodDefinition || (method_arguments != null))
218 throw new InvalidOperationException ("Method is not a generic method definition");
220 if (methodInstantiation == null)
221 throw new ArgumentNullException ("methodInstantiation");
223 if (base_method.GetGenericArguments ().Length != methodInstantiation.Length)
224 throw new ArgumentException ("Incorrect length", "methodInstantiation");
226 foreach (Type type in methodInstantiation) {
228 throw new ArgumentNullException ("methodInstantiation");
231 return new MethodOnTypeBuilderInst (this, methodInstantiation);
234 public override Type [] GetGenericArguments ()
236 if (!base_method.IsGenericMethodDefinition)
238 Type[] source = method_arguments ?? base_method.GetGenericArguments ();
239 Type[] result = new Type [source.Length];
240 source.CopyTo (result, 0);
244 public override MethodInfo GetGenericMethodDefinition ()
246 return generic_method_definition ?? base_method;
249 public override bool ContainsGenericParameters {
251 if (base_method.ContainsGenericParameters)
253 if (!base_method.IsGenericMethodDefinition)
254 throw new NotSupportedException ();
255 if (method_arguments == null)
257 foreach (Type t in method_arguments) {
258 if (t.ContainsGenericParameters)
265 public override bool IsGenericMethodDefinition {
267 return base_method.IsGenericMethodDefinition && method_arguments == null;
271 public override bool IsGenericMethod {
273 return base_method.IsGenericMethodDefinition;
278 // MethodInfo members
281 public override MethodInfo GetBaseDefinition ()
283 throw new NotSupportedException ();
286 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
288 throw new NotSupportedException ();