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
43 MonoGenericClass instantiation;
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 bool IsCompilerContext {
65 get { return ((ModuleBuilder)base_method.Module).assemblyb.IsCompilerContext; }
72 public override Type DeclaringType {
78 public override string Name {
80 return base_method.Name;
84 public override Type ReflectedType {
90 public override Type ReturnType {
92 if (!IsCompilerContext)
93 return base_method.ReturnType;
94 return instantiation.InflateType (base_method.ReturnType, method_arguments);
98 public override bool IsDefined (Type attributeType, bool inherit)
100 throw new NotSupportedException ();
103 public override object [] GetCustomAttributes (bool inherit)
105 throw new NotSupportedException ();
108 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
110 throw new NotSupportedException ();
113 public override string ToString ()
115 //IEnumerable`1 get_Item(TKey)
116 StringBuilder sb = new StringBuilder (ReturnType.ToString ());
118 sb.Append (base_method.Name);
120 if (IsCompilerContext) {
121 ParameterInfo [] par = GetParameters ();
122 for (int i = 0; i < par.Length; ++i) {
125 sb.Append (par [i].ParameterType);
129 return sb.ToString ();
132 // MethodBase members
135 public override MethodImplAttributes GetMethodImplementationFlags ()
137 return base_method.GetMethodImplementationFlags ();
140 public override ParameterInfo [] GetParameters ()
142 if (!IsCompilerContext)
143 throw new NotSupportedException ();
145 ParameterInfo [] res = new ParameterInfo [base_method.parameters.Length];
146 for (int i = 0; i < base_method.parameters.Length; i++) {
147 Type type = instantiation.InflateType (base_method.parameters [i], method_arguments);
148 res [i] = new ParameterInfo (base_method.pinfo == null ? null : base_method.pinfo [i + 1], type, this, i + 1);
153 public override int MetadataToken {
155 if (!IsCompilerContext)
156 return base.MetadataToken;
157 return base_method.MetadataToken;
161 internal override int GetParameterCount ()
163 return base_method.GetParameterCount ();
166 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
168 throw new NotSupportedException ();
171 public override RuntimeMethodHandle MethodHandle {
173 throw new NotSupportedException ();
177 public override MethodAttributes Attributes {
179 return base_method.Attributes;
183 public override CallingConventions CallingConvention {
185 return base_method.CallingConvention;
189 public override MethodInfo MakeGenericMethod (params Type [] typeArguments)
191 if (base_method.generic_params == null || method_arguments != null)
192 throw new NotSupportedException (); //FIXME is this the right exception?
194 if (typeArguments == null)
195 throw new ArgumentNullException ("typeArguments");
197 foreach (Type t in typeArguments) {
199 throw new ArgumentNullException ("typeArguments");
202 if (base_method.generic_params.Length != typeArguments.Length)
203 throw new ArgumentException ("Invalid argument array length");
205 return new MethodOnTypeBuilderInst (this, typeArguments);
208 public override Type [] GetGenericArguments ()
210 if (base_method.generic_params == null)
212 Type[] source = method_arguments ?? base_method.generic_params;
213 Type[] result = new Type [source.Length];
214 source.CopyTo (result, 0);
218 public override MethodInfo GetGenericMethodDefinition ()
220 return (MethodInfo)generic_method_definition ?? base_method;
223 public override bool ContainsGenericParameters {
225 if (base_method.generic_params == null)
226 throw new NotSupportedException ();
227 if (method_arguments == null)
229 foreach (Type t in method_arguments) {
230 if (t.ContainsGenericParameters)
237 public override bool IsGenericMethodDefinition {
239 return base_method.generic_params != null && method_arguments == null;
243 public override bool IsGenericMethod {
245 return base_method.generic_params != null;
250 // MethodInfo members
253 public override MethodInfo GetBaseDefinition ()
255 throw new NotSupportedException ();
258 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
260 throw new NotSupportedException ();