2 // Permission is hereby granted, free of charge, to any person obtaining
3 // a copy of this software and associated documentation files (the
4 // "Software"), to deal in the Software without restriction, including
5 // without limitation the rights to use, copy, modify, merge, publish,
6 // distribute, sublicense, and/or sell copies of the Software, and to
7 // permit persons to whom the Software is furnished to do so, subject to
8 // the following conditions:
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 // Copyright (C) Lluis Sanchez Gual, 2004
26 using System.Reflection;
27 using System.Reflection.Emit;
29 namespace Mono.CodeGeneration
31 public class CodeMethodCall: CodeExpression
33 CodeExpression target;
34 CodeExpression[] parameters;
36 CodeMethod codeMethod;
38 public CodeMethodCall (CodeExpression target, string name, params CodeExpression[] parameters)
41 this.parameters = parameters;
42 Type[] types = GetParameterTypes (parameters);
43 method = target.GetResultType().GetMethod (name, types);
45 throw new InvalidOperationException ("Method " + GetSignature(target.GetResultType(), name, parameters) + " not found");
49 public CodeMethodCall (CodeExpression target, MethodBase method, params CodeExpression[] parameters)
52 this.parameters = parameters;
56 public CodeMethodCall (CodeExpression target, CodeMethod method, params CodeExpression[] parameters)
59 this.parameters = parameters;
60 this.codeMethod = method;
63 public CodeMethodCall (Type type, string name, params CodeExpression[] parameters)
65 this.parameters = parameters;
66 method = type.GetMethod (name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, GetParameterTypes (parameters), null);
67 if (method == null) throw new InvalidOperationException ("Method " + GetSignature(type, name, parameters) + " not found");
70 public CodeMethodCall (MethodInfo method, params CodeExpression[] parameters)
72 this.parameters = parameters;
76 public CodeMethodCall (CodeMethod method, params CodeExpression[] parameters)
78 this.parameters = parameters;
79 this.codeMethod = method;
82 Type[] GetParameterTypes (CodeExpression[] parameters)
84 Type[] ts = new Type [parameters.Length];
85 for (int n=0; n<ts.Length; n++)
86 ts [n] = parameters[n].GetResultType ();
90 string GetSignature (Type type, string name, params CodeExpression[] parameters)
92 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
93 sb.Append (type.FullName).Append(".").Append(name);
95 Type[] types = GetParameterTypes (parameters);
97 for (int n=0; n<types.Length; n++)
99 if (n > 0) sb.Append (", ");
100 sb.Append (types[n].FullName);
103 return sb.ToString ();
106 public override void Generate (ILGenerator gen)
108 if (codeMethod != null)
109 CodeGenerationHelper.GenerateMethodCall (gen, target, codeMethod, parameters);
111 CodeGenerationHelper.GenerateMethodCall (gen, target, method, parameters);
114 public override void GenerateAsStatement (ILGenerator gen)
117 if (GetResultType () != typeof(void)) gen.Emit (OpCodes.Pop);
120 public override void PrintCode (CodeWriter cp)
122 MethodBase met = method != null ? method : codeMethod.MethodInfo;
123 if (!object.ReferenceEquals (target, null))
124 target.PrintCode (cp);
126 cp.Write (met.DeclaringType.FullName);
129 cp.Write (met.Name).Write (" (");
130 for (int n=0; n<parameters.Length; n++) {
131 if (n > 0) cp.Write (", ");
132 parameters[n].PrintCode (cp);
137 public override Type GetResultType ()
139 if (codeMethod != null) return codeMethod.ReturnType;
140 else if (method is MethodInfo) return ((MethodInfo) method).ReturnType;
141 else return typeof (void);