CodeMethodCall.cs 4.6 KB

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