Module.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // System.Reflection/Module.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Runtime.Serialization;
  30. using System.Security.Cryptography.X509Certificates;
  31. using System.Runtime.InteropServices;
  32. using System.Runtime.CompilerServices;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. using System.Collections.Generic;
  36. namespace System.Reflection {
  37. internal enum ResolveTokenError {
  38. OutOfRange,
  39. BadTable,
  40. Other
  41. };
  42. [Serializable]
  43. [StructLayout (LayoutKind.Sequential)]
  44. partial class Module {
  45. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  46. internal static extern int get_MetadataToken (Module module);
  47. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  48. internal static extern int GetMDStreamVersion (IntPtr module_handle);
  49. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  50. internal extern Type[] InternalGetTypes ();
  51. internal Guid MvId {
  52. get {
  53. return GetModuleVersionId ();
  54. }
  55. }
  56. internal Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
  57. if (error == ResolveTokenError.OutOfRange)
  58. return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, Name));
  59. else
  60. return new ArgumentException (String.Format ("Token 0x{0:x} is not a valid {1} token in the scope of module {2}", metadataToken, tokenType, Name), "metadataToken");
  61. }
  62. internal IntPtr[] ptrs_from_types (Type[] types) {
  63. if (types == null)
  64. return null;
  65. else {
  66. IntPtr[] res = new IntPtr [types.Length];
  67. for (int i = 0; i < types.Length; ++i) {
  68. if (types [i] == null)
  69. throw new ArgumentException ();
  70. res [i] = types [i].TypeHandle.Value;
  71. }
  72. return res;
  73. }
  74. }
  75. internal static Type MonoDebugger_ResolveType (Module module, int token)
  76. {
  77. ResolveTokenError error;
  78. IntPtr handle = ResolveTypeToken (module.GetImpl (), token, null, null, out error);
  79. if (handle == IntPtr.Zero)
  80. return null;
  81. else
  82. return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
  83. }
  84. // Used by mcs, the symbol writer, and mdb through reflection
  85. internal static Guid Mono_GetGuid (Module module)
  86. {
  87. return module.GetModuleVersionId ();
  88. }
  89. internal virtual Guid GetModuleVersionId ()
  90. {
  91. return new Guid (GetGuidInternal ());
  92. }
  93. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  94. internal extern IntPtr GetHINSTANCE ();
  95. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  96. private extern string GetGuidInternal ();
  97. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  98. internal extern Type GetGlobalType ();
  99. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  100. internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  101. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  102. internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  103. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  104. internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  105. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  106. internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
  107. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  108. internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  109. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  110. internal static extern byte[] ResolveSignature (IntPtr module, int metadataToken, out ResolveTokenError error);
  111. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  112. internal static extern void GetPEKind (IntPtr module, out PortableExecutableKinds peKind, out ImageFileMachine machine);
  113. public virtual X509Certificate GetSignerCertificate ()
  114. {
  115. throw NotImplemented.ByDesign;
  116. }
  117. internal virtual IntPtr GetImpl () {
  118. throw NotImplemented.ByDesign;
  119. }
  120. }
  121. }