SignatureHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. // System.Reflection.Emit/SignatureHelper.cs
  25. //
  26. // Author:
  27. // Paolo Molaro ([email protected])
  28. //
  29. // (C) 2001 Ximian, Inc. http://www.ximian.com
  30. //
  31. using System;
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. using System.Globalization;
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. namespace System.Reflection.Emit {
  38. #if NET_2_0
  39. [ComVisible (true)]
  40. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  41. [ComDefaultInterfaceAttribute (typeof (_SignatureHelper))]
  42. #endif
  43. public sealed class SignatureHelper {
  44. internal enum SignatureHelperType {
  45. HELPER_FIELD,
  46. HELPER_LOCAL,
  47. HELPER_METHOD,
  48. HELPER_PROPERTY
  49. }
  50. private ModuleBuilder module;
  51. private Type[] arguments;
  52. private SignatureHelperType type;
  53. private Type returnType;
  54. private CallingConventions callConv;
  55. private CallingConvention unmanagedCallConv;
  56. internal SignatureHelper (ModuleBuilder module, SignatureHelperType type)
  57. {
  58. this.type = type;
  59. this.module = module;
  60. }
  61. public static SignatureHelper GetFieldSigHelper (Module mod)
  62. {
  63. if (!(mod is ModuleBuilder))
  64. throw new NotImplementedException ();
  65. return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_FIELD);
  66. }
  67. public static SignatureHelper GetLocalVarSigHelper (Module mod)
  68. {
  69. if (!(mod is ModuleBuilder))
  70. throw new NotImplementedException ();
  71. return new SignatureHelper ((ModuleBuilder) mod, SignatureHelperType.HELPER_LOCAL);
  72. }
  73. public static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callingConvention, Type returnType)
  74. {
  75. return GetMethodSigHelper (mod, callingConvention, (CallingConvention)0, returnType, null);
  76. }
  77. public static SignatureHelper GetMethodSigHelper( Module mod, CallingConvention unmanagedCallingConvention, Type returnType)
  78. {
  79. return GetMethodSigHelper (mod, CallingConventions.Standard, unmanagedCallingConvention, returnType, null);
  80. }
  81. public static SignatureHelper GetMethodSigHelper( Module mod, Type returnType, Type[] parameterTypes)
  82. {
  83. return GetMethodSigHelper (mod, CallingConventions.Standard,
  84. (CallingConvention)0, returnType,
  85. parameterTypes);
  86. }
  87. [MonoTODO]
  88. public static SignatureHelper GetPropertySigHelper( Module mod, Type returnType, Type[] parameterTypes)
  89. {
  90. throw new NotImplementedException ();
  91. }
  92. public void AddArgument (Type clsArgument)
  93. {
  94. if (arguments != null) {
  95. Type[] new_a = new Type [arguments.Length + 1];
  96. System.Array.Copy (arguments, new_a, arguments.Length);
  97. new_a [arguments.Length] = clsArgument;
  98. arguments = new_a;
  99. } else {
  100. arguments = new Type [1];
  101. arguments [0] = clsArgument;
  102. }
  103. }
  104. [MonoTODO]
  105. public void AddSentinel ()
  106. {
  107. throw new NotImplementedException ();
  108. }
  109. [MonoTODO]
  110. public override bool Equals (object obj)
  111. {
  112. throw new NotImplementedException ();
  113. }
  114. [MonoTODO]
  115. public override int GetHashCode ()
  116. {
  117. throw new NotImplementedException ();
  118. }
  119. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  120. internal extern byte[] get_signature_local ();
  121. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  122. internal extern byte[] get_signature_field ();
  123. public byte[] GetSignature ()
  124. {
  125. switch (type) {
  126. case SignatureHelperType.HELPER_LOCAL:
  127. return get_signature_local ();
  128. case SignatureHelperType.HELPER_FIELD:
  129. return get_signature_field ();
  130. default:
  131. throw new NotImplementedException ();
  132. }
  133. }
  134. public override string ToString() {
  135. return "SignatureHelper";
  136. }
  137. internal static SignatureHelper GetMethodSigHelper( Module mod, CallingConventions callConv, CallingConvention unmanagedCallConv, Type returnType,
  138. Type [] parameters)
  139. {
  140. if (!(mod is ModuleBuilder))
  141. throw new NotImplementedException ();
  142. SignatureHelper helper =
  143. new SignatureHelper ((ModuleBuilder)mod, SignatureHelperType.HELPER_METHOD);
  144. helper.returnType = returnType;
  145. helper.callConv = callConv;
  146. helper.unmanagedCallConv = unmanagedCallConv;
  147. if (parameters != null) {
  148. helper.arguments = new Type [parameters.Length];
  149. for (int i = 0; i < parameters.Length; ++i)
  150. helper.arguments [i] = parameters [i];
  151. }
  152. return helper;
  153. }
  154. }
  155. }