CodeMethodCall.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  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:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  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.
  20. //
  21. // Copyright (C) Lluis Sanchez Gual, 2004
  22. //
  23. using System;
  24. using System.Reflection;
  25. using System.Reflection.Emit;
  26. namespace Mono.CodeGeneration
  27. {
  28. public class CodeMethodCall: CodeExpression
  29. {
  30. CodeExpression target;
  31. CodeExpression[] parameters;
  32. MethodBase method;
  33. CodeMethod codeMethod;
  34. public CodeMethodCall (CodeExpression target, string name, params CodeExpression[] parameters)
  35. {
  36. this.target = target;
  37. this.parameters = parameters;
  38. Type[] types = GetParameterTypes (parameters);
  39. method = target.GetResultType().GetMethod (name, types);
  40. if (method == null) {
  41. throw new InvalidOperationException ("Method " + GetSignature(target.GetResultType(), name, parameters) + " not found");
  42. }
  43. }
  44. public CodeMethodCall (CodeExpression target, MethodBase method, params CodeExpression[] parameters)
  45. {
  46. this.target = target;
  47. this.parameters = parameters;
  48. this.method = method;
  49. }
  50. public CodeMethodCall (CodeExpression target, CodeMethod method, params CodeExpression[] parameters)
  51. {
  52. this.target = target;
  53. this.parameters = parameters;
  54. this.codeMethod = method;
  55. }
  56. public CodeMethodCall (Type type, string name, params CodeExpression[] parameters)
  57. {
  58. this.parameters = parameters;
  59. method = type.GetMethod (name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, GetParameterTypes (parameters), null);
  60. if (method == null) throw new InvalidOperationException ("Method " + GetSignature(type, name, parameters) + " not found");
  61. }
  62. public CodeMethodCall (MethodInfo method, params CodeExpression[] parameters)
  63. {
  64. this.parameters = parameters;
  65. this.method = method;
  66. }
  67. public CodeMethodCall (CodeMethod method, params CodeExpression[] parameters)
  68. {
  69. this.parameters = parameters;
  70. this.codeMethod = method;
  71. }
  72. Type[] GetParameterTypes (CodeExpression[] parameters)
  73. {
  74. Type[] ts = new Type [parameters.Length];
  75. for (int n=0; n<ts.Length; n++)
  76. ts [n] = parameters[n].GetResultType ();
  77. return ts;
  78. }
  79. string GetSignature (Type type, string name, params CodeExpression[] parameters)
  80. {
  81. System.Text.StringBuilder sb = new System.Text.StringBuilder ();
  82. sb.Append (type.FullName).Append(".").Append(name);
  83. Type[] types = GetParameterTypes (parameters);
  84. sb.Append ("(");
  85. for (int n=0; n<types.Length; n++)
  86. {
  87. if (n > 0) sb.Append (", ");
  88. sb.Append (types[n].FullName);
  89. }
  90. sb.Append (")");
  91. return sb.ToString ();
  92. }
  93. public override void Generate (ILGenerator gen)
  94. {
  95. if (codeMethod != null)
  96. CodeGenerationHelper.GenerateMethodCall (gen, target, codeMethod, parameters);
  97. else
  98. CodeGenerationHelper.GenerateMethodCall (gen, target, method, parameters);
  99. }
  100. public override void GenerateAsStatement (ILGenerator gen)
  101. {
  102. Generate (gen);
  103. if (GetResultType () != typeof(void)) gen.Emit (OpCodes.Pop);
  104. }
  105. public override void PrintCode (CodeWriter cp)
  106. {
  107. MethodBase met = method != null ? method : codeMethod.MethodInfo;
  108. if (!object.ReferenceEquals (target, null))
  109. target.PrintCode (cp);
  110. else
  111. cp.Write (met.DeclaringType.FullName);
  112. cp.Write (".");
  113. cp.Write (met.Name).Write (" (");
  114. for (int n=0; n<parameters.Length; n++) {
  115. if (n > 0) cp.Write (", ");
  116. parameters[n].PrintCode (cp);
  117. }
  118. cp.Write (")");
  119. }
  120. public override Type GetResultType ()
  121. {
  122. if (codeMethod != null) return codeMethod.ReturnType;
  123. else if (method is MethodInfo) return ((MethodInfo) method).ReturnType;
  124. else return typeof (void);
  125. }
  126. }
  127. }