2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / Mono.Cecil / Mono.Cecil / MethodReference.cs
blob1e713bc631b66e6d598e0e240060e18edb9a2dfa
1 //
2 // MethodReference.cs
3 //
4 // Author:
5 // Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2005 - 2007 Jb Evain
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace Mono.Cecil {
31 using System.Text;
33 public class MethodReference : MemberReference, IMethodSignature, IGenericParameterProvider {
35 ParameterDefinitionCollection m_parameters;
36 MethodReturnType m_returnType;
38 bool m_hasThis;
39 bool m_explicitThis;
40 MethodCallingConvention m_callConv;
41 GenericParameterCollection m_genparams;
43 public virtual bool HasThis {
44 get { return m_hasThis; }
45 set { m_hasThis = value; }
48 public virtual bool ExplicitThis {
49 get { return m_explicitThis; }
50 set { m_explicitThis = value; }
53 public virtual MethodCallingConvention CallingConvention {
54 get { return m_callConv; }
55 set { m_callConv = value; }
58 public virtual bool HasParameters {
59 get { return (m_parameters == null) ? false : (m_parameters.Count > 0); }
62 public virtual ParameterDefinitionCollection Parameters {
63 get {
64 if (m_parameters == null)
65 m_parameters = new ParameterDefinitionCollection (this);
66 return m_parameters;
70 public bool HasGenericParameters {
71 get { return (m_genparams == null) ? false : (m_genparams.Count > 0); }
74 public GenericParameterCollection GenericParameters {
75 get {
76 if (m_genparams == null)
77 m_genparams = new GenericParameterCollection (this);
78 return m_genparams;
82 public virtual MethodReturnType ReturnType {
83 get { return m_returnType;}
84 set { m_returnType = value; }
87 internal MethodReference (string name, bool hasThis,
88 bool explicitThis, MethodCallingConvention callConv) : this (name)
90 m_parameters = new ParameterDefinitionCollection (this);
91 m_hasThis = hasThis;
92 m_explicitThis = explicitThis;
93 m_callConv = callConv;
96 internal MethodReference (string name) : base (name)
98 m_returnType = new MethodReturnType (null);
101 public MethodReference (string name,
102 TypeReference declaringType, TypeReference returnType,
103 bool hasThis, bool explicitThis, MethodCallingConvention callConv) :
104 this (name, hasThis, explicitThis, callConv)
106 this.DeclaringType = declaringType;
107 this.ReturnType.ReturnType = returnType;
110 public virtual MethodDefinition Resolve ()
112 TypeReference declaringType = DeclaringType;
113 if (declaringType == null)
114 return null;
116 return declaringType.Module.Resolver.Resolve (this);
119 public virtual MethodReference GetOriginalMethod ()
121 return this;
124 public int GetSentinel ()
126 if (HasParameters) {
127 for (int i = 0; i < Parameters.Count; i++)
128 if (Parameters [i].ParameterType is SentinelType)
129 return i;
131 return -1;
134 public override string ToString ()
136 int sentinel = GetSentinel ();
138 StringBuilder sb = new StringBuilder ();
139 sb.Append (m_returnType.ReturnType.FullName);
140 sb.Append (" ");
141 sb.Append (base.ToString ());
142 sb.Append ("(");
143 if (this.HasParameters) {
144 for (int i = 0; i < this.Parameters.Count; i++) {
145 if (i > 0)
146 sb.Append (",");
148 if (i == sentinel)
149 sb.Append ("...,");
151 sb.Append (this.Parameters [i].ParameterType.FullName);
154 sb.Append (")");
155 return sb.ToString ();