Merge pull request #237 from knocte/master
[mono.git] / mcs / class / IKVM.Reflection / MethodInfo.cs
1 /*
2   Copyright (C) 2009 Jeroen Frijters
3
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.
7
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:
11
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.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Text;
26
27 namespace IKVM.Reflection
28 {
29         public abstract class MethodInfo : MethodBase, IGenericContext, IGenericBinder
30         {
31                 // prevent external subclasses
32                 internal MethodInfo()
33                 {
34                 }
35
36                 public sealed override MemberTypes MemberType
37                 {
38                         get { return MemberTypes.Method; }
39                 }
40
41                 public abstract Type ReturnType { get; }
42                 public abstract ParameterInfo ReturnParameter { get; }
43
44                 public virtual MethodInfo MakeGenericMethod(params Type[] typeArguments)
45                 {
46                         throw new NotSupportedException(this.GetType().FullName);
47                 }
48
49                 public virtual MethodInfo GetGenericMethodDefinition()
50                 {
51                         throw new NotSupportedException(this.GetType().FullName);
52                 }
53
54                 public override string ToString()
55                 {
56                         StringBuilder sb = new StringBuilder();
57                         sb.Append(this.ReturnType.Name).Append(' ').Append(this.Name);
58                         string sep;
59                         if (this.IsGenericMethod)
60                         {
61                                 sb.Append('[');
62                                 sep = "";
63                                 foreach (Type arg in GetGenericArguments())
64                                 {
65                                         sb.Append(sep).Append(arg);
66                                         sep = ", ";
67                                 }
68                                 sb.Append(']');
69                         }
70                         sb.Append('(');
71                         sep = "";
72                         foreach (ParameterInfo arg in GetParameters())
73                         {
74                                 sb.Append(sep).Append(arg.ParameterType);
75                                 sep = ", ";
76                         }
77                         sb.Append(')');
78                         return sb.ToString();
79                 }
80
81                 internal bool IsNewSlot
82                 {
83                         get { return (this.Attributes & MethodAttributes.NewSlot) != 0; }
84                 }
85
86                 public MethodInfo GetBaseDefinition()
87                 {
88                         MethodInfo match = this;
89                         if (match.IsVirtual)
90                         {
91                                 for (Type type = this.DeclaringType.BaseType; type != null && !match.IsNewSlot; type = type.BaseType)
92                                 {
93                                         MethodInfo method = type.FindMethod(this.Name, this.MethodSignature) as MethodInfo;
94                                         if (method != null && method.IsVirtual)
95                                         {
96                                                 match = method;
97                                         }
98                                 }
99                         }
100                         return match;
101                 }
102
103                 public virtual MethodInfo[] __GetMethodImpls()
104                 {
105                         throw new NotSupportedException();
106                 }
107
108                 Type IGenericContext.GetGenericTypeArgument(int index)
109                 {
110                         return this.DeclaringType.GetGenericTypeArgument(index);
111                 }
112
113                 Type IGenericContext.GetGenericMethodArgument(int index)
114                 {
115                         return GetGenericMethodArgument(index);
116                 }
117
118                 internal virtual Type GetGenericMethodArgument(int index)
119                 {
120                         throw new InvalidOperationException();
121                 }
122
123                 internal virtual int GetGenericMethodArgumentCount()
124                 {
125                         throw new InvalidOperationException();
126                 }
127
128                 internal override MethodInfo GetMethodOnTypeDefinition()
129                 {
130                         return this;
131                 }
132
133                 Type IGenericBinder.BindTypeParameter(Type type)
134                 {
135                         return this.DeclaringType.GetGenericTypeArgument(type.GenericParameterPosition);
136                 }
137
138                 Type IGenericBinder.BindMethodParameter(Type type)
139                 {
140                         return GetGenericMethodArgument(type.GenericParameterPosition);
141                 }
142
143                 internal override MethodBase BindTypeParameters(Type type)
144                 {
145                         return new GenericMethodInstance(this.DeclaringType.BindTypeParameters(type), this, null);
146                 }
147
148                 // This method is used by ILGenerator and exists to allow ArrayMethod to override it,
149                 // because ArrayMethod doesn't have a working MethodAttributes property, so it needs
150                 // to base the result of this on the CallingConvention.
151                 internal virtual bool HasThis
152                 {
153                         get { return !IsStatic; }
154                 }
155         }
156 }