2 Copyright (C) 2009-2012 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Collections.Generic;
26 using System.Diagnostics;
29 namespace IKVM.Reflection
31 public abstract class MethodInfo : MethodBase, IGenericContext, IGenericBinder
33 // prevent external subclasses
38 public sealed override MemberTypes MemberType
40 get { return MemberTypes.Method; }
43 public abstract Type ReturnType { get; }
44 public abstract ParameterInfo ReturnParameter { get; }
46 public virtual MethodInfo MakeGenericMethod(params Type[] typeArguments)
48 throw new NotSupportedException(this.GetType().FullName);
51 public virtual MethodInfo GetGenericMethodDefinition()
53 throw new NotSupportedException(this.GetType().FullName);
56 public override string ToString()
58 StringBuilder sb = new StringBuilder();
59 sb.Append(this.ReturnType.Name).Append(' ').Append(this.Name);
61 if (this.IsGenericMethod)
65 foreach (Type arg in GetGenericArguments())
67 sb.Append(sep).Append(arg);
74 foreach (ParameterInfo arg in GetParameters())
76 sb.Append(sep).Append(arg.ParameterType);
83 internal bool IsNewSlot
85 get { return (this.Attributes & MethodAttributes.NewSlot) != 0; }
88 public MethodInfo GetBaseDefinition()
90 MethodInfo match = this;
93 for (Type type = this.DeclaringType.BaseType; type != null && !match.IsNewSlot; type = type.BaseType)
95 MethodInfo method = type.FindMethod(this.Name, this.MethodSignature) as MethodInfo;
96 if (method != null && method.IsVirtual)
105 public virtual MethodInfo[] __GetMethodImpls()
107 throw new NotSupportedException();
110 Type IGenericContext.GetGenericTypeArgument(int index)
112 return this.DeclaringType.GetGenericTypeArgument(index);
115 Type IGenericContext.GetGenericMethodArgument(int index)
117 return GetGenericMethodArgument(index);
120 internal virtual Type GetGenericMethodArgument(int index)
122 throw new InvalidOperationException();
125 internal virtual int GetGenericMethodArgumentCount()
127 throw new InvalidOperationException();
130 internal override MethodInfo GetMethodOnTypeDefinition()
135 Type IGenericBinder.BindTypeParameter(Type type)
137 return this.DeclaringType.GetGenericTypeArgument(type.GenericParameterPosition);
140 Type IGenericBinder.BindMethodParameter(Type type)
142 return GetGenericMethodArgument(type.GenericParameterPosition);
145 internal override MethodBase BindTypeParameters(Type type)
147 return new GenericMethodInstance(this.DeclaringType.BindTypeParameters(type), this, null);
150 // This method is used by ILGenerator and exists to allow ArrayMethod to override it,
151 // because ArrayMethod doesn't have a working MethodAttributes property, so it needs
152 // to base the result of this on the CallingConvention.
153 internal virtual bool HasThis
155 get { return !IsStatic; }
158 internal sealed override MemberInfo SetReflectedType(Type type)
160 return new MethodInfoWithReflectedType(type, this);
164 sealed class MethodInfoWithReflectedType : MethodInfo
166 private readonly Type reflectedType;
167 private readonly MethodInfo method;
169 internal MethodInfoWithReflectedType(Type reflectedType, MethodInfo method)
171 Debug.Assert(reflectedType != method.DeclaringType);
172 this.reflectedType = reflectedType;
173 this.method = method;
176 public override bool Equals(object obj)
178 MethodInfoWithReflectedType other = obj as MethodInfoWithReflectedType;
180 && other.reflectedType == reflectedType
181 && other.method == method;
184 public override int GetHashCode()
186 return reflectedType.GetHashCode() ^ method.GetHashCode();
189 internal override MethodSignature MethodSignature
191 get { return method.MethodSignature; }
194 internal override int ParameterCount
196 get { return method.ParameterCount; }
199 public override ParameterInfo[] GetParameters()
201 ParameterInfo[] parameters = method.GetParameters();
202 for (int i = 0; i < parameters.Length; i++)
204 parameters[i] = new ParameterInfoWrapper(this, parameters[i]);
209 public override MethodAttributes Attributes
211 get { return method.Attributes; }
214 public override MethodImplAttributes GetMethodImplementationFlags()
216 return method.GetMethodImplementationFlags();
219 public override MethodBody GetMethodBody()
221 return method.GetMethodBody();
224 public override CallingConventions CallingConvention
226 get { return method.CallingConvention; }
229 public override int __MethodRVA
231 get { return method.__MethodRVA; }
234 public override Type ReturnType
236 get { return method.ReturnType; }
239 public override ParameterInfo ReturnParameter
241 get { return new ParameterInfoWrapper(this, method.ReturnParameter); }
244 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
246 return (MethodInfo)method.MakeGenericMethod(typeArguments).SetReflectedType(reflectedType);
249 public override MethodInfo GetGenericMethodDefinition()
251 return (MethodInfo)method.GetGenericMethodDefinition().SetReflectedType(reflectedType);
254 public override string ToString()
256 return method.ToString();
259 public override MethodInfo[] __GetMethodImpls()
261 return method.__GetMethodImpls();
264 internal override Type GetGenericMethodArgument(int index)
266 return method.GetGenericMethodArgument(index);
269 internal override int GetGenericMethodArgumentCount()
271 return method.GetGenericMethodArgumentCount();
274 internal override MethodInfo GetMethodOnTypeDefinition()
276 return method.GetMethodOnTypeDefinition();
279 internal override bool HasThis
281 get { return method.HasThis; }
284 public override Module Module
286 get { return method.Module; }
289 public override Type DeclaringType
291 get { return method.DeclaringType; }
294 public override Type ReflectedType
296 get { return reflectedType; }
299 public override string Name
301 get { return method.Name; }
304 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
306 return method.ImportTo(module);
309 public override MethodBase __GetMethodOnTypeDefinition()
311 return method.__GetMethodOnTypeDefinition();
314 public override bool __IsMissing
316 get { return method.__IsMissing; }
319 internal override MethodBase BindTypeParameters(Type type)
321 return method.BindTypeParameters(type);
324 public override bool ContainsGenericParameters
326 get { return method.ContainsGenericParameters; }
329 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
331 return method.GetCustomAttributesData(attributeType);
334 public override Type[] GetGenericArguments()
336 return method.GetGenericArguments();
339 public override bool IsGenericMethod
341 get { return method.IsGenericMethod; }
344 public override bool IsGenericMethodDefinition
346 get { return method.IsGenericMethodDefinition; }
349 public override int MetadataToken
351 get { return method.MetadataToken; }