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.
31 using System.Globalization;
32 using System.Reflection;
34 using System.Runtime.InteropServices;
37 namespace System.Reflection.Emit
40 * This class represents a method of an instantiation of a generic type builder.
42 [StructLayout (LayoutKind.Sequential)]
43 internal class MethodOnTypeBuilderInst : MethodInfo
45 #region Keep in sync with object-internals.h
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;
50 MethodInfo generic_method_definition;
52 public MethodOnTypeBuilderInst (MonoGenericClass instantiation, MethodInfo base_method)
54 this.instantiation = instantiation;
55 this.base_method = base_method;
58 internal MethodOnTypeBuilderInst (MethodOnTypeBuilderInst gmd, Type[] typeArguments)
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;
67 internal MethodOnTypeBuilderInst (MethodInfo method, Type[] typeArguments)
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;
77 static MethodInfo ExtractBaseMethod (MethodInfo info)
79 if (info is MethodBuilder)
81 if (info is MethodOnTypeBuilderInst)
82 return ((MethodOnTypeBuilderInst)info).base_method;
84 if (info.IsGenericMethod)
85 info = info.GetGenericMethodDefinition ();
87 Type t = info.DeclaringType;
88 if (!t.IsGenericType || t.IsGenericTypeDefinition)
91 return (MethodInfo)t.Module.ResolveMethod (info.MetadataToken);
94 internal Type[] GetTypeArgs ()
96 if (!instantiation.IsGenericType || instantiation.IsGenericParameter)
99 return instantiation.GetGenericArguments ();
103 // MemberInfo members
106 public override Type DeclaringType {
108 return instantiation;
112 public override string Name {
114 return base_method.Name;
118 public override Type ReflectedType {
120 return instantiation;
124 public override Type ReturnType {
126 return base_method.ReturnType;
130 public override bool IsDefined (Type attributeType, bool inherit)
132 throw new NotSupportedException ();
135 public override object [] GetCustomAttributes (bool inherit)
137 throw new NotSupportedException ();
140 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
142 throw new NotSupportedException ();
145 public override string ToString ()
147 //IEnumerable`1 get_Item(TKey)
148 StringBuilder sb = new StringBuilder (ReturnType.ToString ());
150 sb.Append (base_method.Name);
153 return sb.ToString ();
156 // MethodBase members
159 public override MethodImplAttributes GetMethodImplementationFlags ()
161 return base_method.GetMethodImplementationFlags ();
164 public override ParameterInfo [] GetParameters ()
166 throw new NotSupportedException ();
169 public override int MetadataToken {
171 return base.MetadataToken;
175 internal override int GetParameterCount ()
177 return base_method.GetParameterCount ();
180 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
182 throw new NotSupportedException ();
185 public override RuntimeMethodHandle MethodHandle {
187 throw new NotSupportedException ();
191 public override MethodAttributes Attributes {
193 return base_method.Attributes;
197 public override CallingConventions CallingConvention {
199 return base_method.CallingConvention;
203 public override MethodInfo MakeGenericMethod (params Type [] methodInstantiation)
205 if (!base_method.IsGenericMethodDefinition || (method_arguments != null))
206 throw new InvalidOperationException ("Method is not a generic method definition");
208 if (methodInstantiation == null)
209 throw new ArgumentNullException ("methodInstantiation");
211 if (base_method.GetGenericArguments ().Length != methodInstantiation.Length)
212 throw new ArgumentException ("Incorrect length", "methodInstantiation");
214 foreach (Type type in methodInstantiation) {
216 throw new ArgumentNullException ("methodInstantiation");
219 return new MethodOnTypeBuilderInst (this, methodInstantiation);
222 public override Type [] GetGenericArguments ()
224 if (!base_method.IsGenericMethodDefinition)
226 Type[] source = method_arguments ?? base_method.GetGenericArguments ();
227 Type[] result = new Type [source.Length];
228 source.CopyTo (result, 0);
232 public override MethodInfo GetGenericMethodDefinition ()
234 return generic_method_definition ?? base_method;
237 public override bool ContainsGenericParameters {
239 if (base_method.ContainsGenericParameters)
241 if (!base_method.IsGenericMethodDefinition)
242 throw new NotSupportedException ();
243 if (method_arguments == null)
245 foreach (Type t in method_arguments) {
246 if (t.ContainsGenericParameters)
253 public override bool IsGenericMethodDefinition {
255 return base_method.IsGenericMethodDefinition && method_arguments == null;
259 public override bool IsGenericMethod {
261 return base_method.IsGenericMethodDefinition;
266 // MethodInfo members
269 public override MethodInfo GetBaseDefinition ()
271 throw new NotSupportedException ();
274 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
276 throw new NotSupportedException ();