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;
35 namespace System.Reflection.Emit
38 * This class represents a method of an instantiation of a generic type builder.
40 internal class MethodOnTypeBuilderInst : MethodInfo
42 #region Keep in sync with object-internals.h
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;
49 public MethodOnTypeBuilderInst (MonoGenericClass instantiation, MethodBuilder base_method)
51 this.instantiation = instantiation;
52 this.base_method = base_method;
55 internal MethodOnTypeBuilderInst (MethodOnTypeBuilderInst gmd, Type[] typeArguments)
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;
64 internal Type[] GetTypeArgs ()
66 if (!instantiation.IsGenericType || instantiation.IsGenericParameter)
69 return instantiation.GetGenericArguments ();
72 internal bool IsCompilerContext {
73 get { return ((ModuleBuilder)base_method.Module).assemblyb.IsCompilerContext; }
80 public override Type DeclaringType {
86 public override string Name {
88 return base_method.Name;
92 public override Type ReflectedType {
98 public override Type ReturnType {
100 if (!IsCompilerContext)
101 return base_method.ReturnType;
102 return MonoGenericClass.InflateType (base_method.ReturnType, GetTypeArgs (), method_arguments);
106 public override bool IsDefined (Type attributeType, bool inherit)
108 throw new NotSupportedException ();
111 public override object [] GetCustomAttributes (bool inherit)
113 throw new NotSupportedException ();
116 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
118 throw new NotSupportedException ();
121 public override string ToString ()
123 //IEnumerable`1 get_Item(TKey)
124 StringBuilder sb = new StringBuilder (ReturnType.ToString ());
126 sb.Append (base_method.Name);
128 if (IsCompilerContext) {
129 ParameterInfo [] par = GetParameters ();
130 for (int i = 0; i < par.Length; ++i) {
133 sb.Append (par [i].ParameterType);
137 return sb.ToString ();
140 // MethodBase members
143 public override MethodImplAttributes GetMethodImplementationFlags ()
145 return base_method.GetMethodImplementationFlags ();
148 public override ParameterInfo [] GetParameters ()
150 if (!IsCompilerContext)
151 throw new NotSupportedException ();
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);
161 public override int MetadataToken {
163 if (!IsCompilerContext)
164 return base.MetadataToken;
165 return base_method.MetadataToken;
169 internal override int GetParameterCount ()
171 return base_method.GetParameterCount ();
174 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
176 throw new NotSupportedException ();
179 public override RuntimeMethodHandle MethodHandle {
181 throw new NotSupportedException ();
185 public override MethodAttributes Attributes {
187 return base_method.Attributes;
191 public override CallingConventions CallingConvention {
193 return base_method.CallingConvention;
197 public override MethodInfo MakeGenericMethod (params Type [] typeArguments)
199 if (base_method.generic_params == null || method_arguments != null)
200 throw new NotSupportedException (); //FIXME is this the right exception?
202 if (typeArguments == null)
203 throw new ArgumentNullException ("typeArguments");
205 foreach (Type t in typeArguments) {
207 throw new ArgumentNullException ("typeArguments");
210 if (base_method.generic_params.Length != typeArguments.Length)
211 throw new ArgumentException ("Invalid argument array length");
213 return new MethodOnTypeBuilderInst (this, typeArguments);
216 public override Type [] GetGenericArguments ()
218 if (base_method.generic_params == null)
220 Type[] source = method_arguments ?? base_method.generic_params;
221 Type[] result = new Type [source.Length];
222 source.CopyTo (result, 0);
226 public override MethodInfo GetGenericMethodDefinition ()
228 return (MethodInfo)generic_method_definition ?? base_method;
231 public override bool ContainsGenericParameters {
233 if (base_method.generic_params == null)
234 throw new NotSupportedException ();
235 if (method_arguments == null)
237 foreach (Type t in method_arguments) {
238 if (t.ContainsGenericParameters)
245 public override bool IsGenericMethodDefinition {
247 return base_method.generic_params != null && method_arguments == null;
251 public override bool IsGenericMethod {
253 return base_method.generic_params != null;
258 // MethodInfo members
261 public override MethodInfo GetBaseDefinition ()
263 throw new NotSupportedException ();
266 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
268 throw new NotSupportedException ();