RuntimeModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. //
  2. // System.Reflection/RuntimeModule.cs
  3. //
  4. // Author:
  5. // Rodrigo Kumpera ([email protected])
  6. //
  7. // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Globalization;
  32. using System.Runtime.InteropServices;
  33. using System.Runtime.CompilerServices;
  34. #if !NETCORE
  35. using System.Security.Cryptography.X509Certificates;
  36. using System.Security;
  37. using System.Security.Permissions;
  38. #endif
  39. using System.Runtime.Serialization;
  40. namespace System.Reflection {
  41. [ComVisible (true)]
  42. #if !NETCORE
  43. [ComDefaultInterfaceAttribute (typeof (_Module))]
  44. #endif
  45. [Serializable]
  46. [ClassInterface(ClassInterfaceType.None)]
  47. [StructLayout (LayoutKind.Sequential)]
  48. class RuntimeModule : Module
  49. {
  50. #pragma warning disable 649
  51. #region Sync with object-internals.h
  52. #region Sync with ModuleBuilder
  53. internal IntPtr _impl; /* a pointer to a MonoImage */
  54. internal Assembly assembly;
  55. internal string fqname;
  56. internal string name;
  57. internal string scopename;
  58. internal bool is_resource;
  59. internal int token;
  60. #endregion
  61. #endregion
  62. #pragma warning restore 649
  63. public
  64. override
  65. Assembly Assembly {
  66. get { return assembly; }
  67. }
  68. public
  69. override
  70. // Note: we do not ask for PathDiscovery because no path is returned here.
  71. // However MS Fx requires it (see FDBK23572 for details).
  72. string Name {
  73. get { return name; }
  74. }
  75. public
  76. override
  77. string ScopeName {
  78. get { return scopename; }
  79. }
  80. public
  81. override
  82. int MDStreamVersion {
  83. get {
  84. if (_impl == IntPtr.Zero)
  85. throw new NotSupportedException ();
  86. return GetMDStreamVersion (_impl);
  87. }
  88. }
  89. public
  90. override
  91. Guid ModuleVersionId {
  92. get {
  93. return GetModuleVersionId ();
  94. }
  95. }
  96. public override
  97. string FullyQualifiedName {
  98. get {
  99. #if !MOBILE && !NETCORE
  100. if (SecurityManager.SecurityEnabled) {
  101. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
  102. }
  103. #endif
  104. return fqname;
  105. }
  106. }
  107. public
  108. override
  109. bool IsResource()
  110. {
  111. return is_resource;
  112. }
  113. public override
  114. Type[] FindTypes(TypeFilter filter, object filterCriteria)
  115. {
  116. var filtered = new List<Type> ();
  117. Type[] types = GetTypes ();
  118. foreach (Type t in types)
  119. if (filter (t, filterCriteria))
  120. filtered.Add (t);
  121. return filtered.ToArray ();
  122. }
  123. public override
  124. object[] GetCustomAttributes(bool inherit)
  125. {
  126. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  127. }
  128. public override
  129. object[] GetCustomAttributes(Type attributeType, bool inherit)
  130. {
  131. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  132. }
  133. public override
  134. FieldInfo GetField (string name, BindingFlags bindingAttr)
  135. {
  136. if (name == null)
  137. throw new ArgumentNullException("name");
  138. if (IsResource ())
  139. return null;
  140. Type globalType = GetGlobalType (_impl);
  141. return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
  142. }
  143. public override
  144. FieldInfo[] GetFields (BindingFlags bindingFlags)
  145. {
  146. if (IsResource ())
  147. return new FieldInfo [0];
  148. Type globalType = GetGlobalType (_impl);
  149. return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
  150. }
  151. public override
  152. int MetadataToken {
  153. get {
  154. return get_MetadataToken (this);
  155. }
  156. }
  157. protected
  158. override
  159. MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  160. {
  161. if (IsResource ())
  162. return null;
  163. Type globalType = GetGlobalType (_impl);
  164. if (globalType == null)
  165. return null;
  166. if (types == null)
  167. return globalType.GetMethod (name);
  168. return globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
  169. }
  170. public
  171. override
  172. MethodInfo[] GetMethods (BindingFlags bindingFlags) {
  173. if (IsResource ())
  174. return new MethodInfo [0];
  175. Type globalType = GetGlobalType (_impl);
  176. return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
  177. }
  178. #if !NETCORE
  179. internal override ModuleHandle GetModuleHandleImpl() => new ModuleHandle (_impl);
  180. #endif
  181. public override
  182. void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
  183. RuntimeModule.GetPEKind (_impl, out peKind, out machine);
  184. }
  185. public override
  186. Type GetType(string className, bool throwOnError, bool ignoreCase)
  187. {
  188. if (className == null)
  189. throw new ArgumentNullException ("className");
  190. if (className == String.Empty)
  191. throw new ArgumentException ("Type name can't be empty");
  192. return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
  193. }
  194. public override
  195. bool IsDefined (Type attributeType, bool inherit)
  196. {
  197. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  198. }
  199. public
  200. override
  201. FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  202. return ResolveField (this, _impl, metadataToken, genericTypeArguments, genericMethodArguments);
  203. }
  204. internal static FieldInfo ResolveField (Module module, IntPtr monoModule, int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  205. ResolveTokenError error;
  206. IntPtr handle = ResolveFieldToken (monoModule, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  207. if (handle == IntPtr.Zero)
  208. throw resolve_token_exception (module.Name, metadataToken, error, "Field");
  209. else
  210. return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
  211. }
  212. public
  213. override
  214. MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  215. return ResolveMember (this, _impl, metadataToken, genericTypeArguments, genericMethodArguments);
  216. }
  217. internal static MemberInfo ResolveMember (Module module, IntPtr monoModule, int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  218. ResolveTokenError error;
  219. MemberInfo m = ResolveMemberToken (monoModule, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  220. if (m == null)
  221. throw resolve_token_exception (module.Name, metadataToken, error, "MemberInfo");
  222. else
  223. return m;
  224. }
  225. public
  226. override
  227. MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  228. return ResolveMethod (this, _impl, metadataToken, genericTypeArguments, genericMethodArguments);
  229. }
  230. internal static MethodBase ResolveMethod (Module module, IntPtr monoModule, int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  231. ResolveTokenError error;
  232. IntPtr handle = ResolveMethodToken (monoModule, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  233. if (handle == IntPtr.Zero)
  234. throw resolve_token_exception (module.Name, metadataToken, error, "MethodBase");
  235. else
  236. return RuntimeMethodInfo.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
  237. }
  238. public
  239. override
  240. string ResolveString (int metadataToken) {
  241. return ResolveString (this, _impl, metadataToken);
  242. }
  243. internal static string ResolveString (Module module, IntPtr monoModule, int metadataToken) {
  244. ResolveTokenError error;
  245. string s = ResolveStringToken (monoModule, metadataToken, out error);
  246. if (s == null)
  247. throw resolve_token_exception (module.Name, metadataToken, error, "string");
  248. else
  249. return s;
  250. }
  251. public
  252. override
  253. Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  254. return ResolveType (this, _impl, metadataToken, genericTypeArguments, genericMethodArguments);
  255. }
  256. internal static Type ResolveType (Module module, IntPtr monoModule, int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  257. ResolveTokenError error;
  258. IntPtr handle = ResolveTypeToken (monoModule, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  259. if (handle == IntPtr.Zero)
  260. throw resolve_token_exception (module.Name, metadataToken, error, "Type");
  261. else
  262. return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
  263. }
  264. public
  265. override
  266. byte[] ResolveSignature (int metadataToken) {
  267. return ResolveSignature (this, _impl, metadataToken);
  268. }
  269. internal static byte[] ResolveSignature (Module module, IntPtr monoModule, int metadataToken) {
  270. ResolveTokenError error;
  271. byte[] res = ResolveSignature (monoModule, metadataToken, out error);
  272. if (res == null)
  273. throw resolve_token_exception (module.Name, metadataToken, error, "signature");
  274. else
  275. return res;
  276. }
  277. #if !NETCORE
  278. public override void GetObjectData (SerializationInfo info, StreamingContext context)
  279. {
  280. if (info == null)
  281. throw new ArgumentNullException ("info");
  282. UnitySerializationHolder.GetUnitySerializationInfo (info, UnitySerializationHolder.ModuleUnity, this.ScopeName, this.GetRuntimeAssembly ());
  283. }
  284. #endif
  285. #if !MOBILE && !NETCORE
  286. public
  287. override
  288. X509Certificate GetSignerCertificate ()
  289. {
  290. try {
  291. return X509Certificate.CreateFromSignedFile (assembly.Location);
  292. }
  293. catch {
  294. return null;
  295. }
  296. }
  297. #endif
  298. public override
  299. Type[] GetTypes()
  300. {
  301. return InternalGetTypes (_impl);
  302. }
  303. public override IList<CustomAttributeData> GetCustomAttributesData () {
  304. return CustomAttributeData.GetCustomAttributes (this);
  305. }
  306. internal RuntimeAssembly GetRuntimeAssembly ()
  307. {
  308. return (RuntimeAssembly)assembly;
  309. }
  310. internal IntPtr MonoModule {
  311. get {
  312. return _impl;
  313. }
  314. }
  315. #if NETCORE
  316. internal Guid GetModuleVersionId ()
  317. #else
  318. internal override Guid GetModuleVersionId ()
  319. #endif
  320. {
  321. return new Guid (GetGuidInternal (_impl));
  322. }
  323. internal static Exception resolve_token_exception (string name, int metadataToken, ResolveTokenError error, string tokenType) {
  324. if (error == ResolveTokenError.OutOfRange)
  325. return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
  326. else
  327. 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");
  328. }
  329. internal static IntPtr[] ptrs_from_types (Type[] types) {
  330. if (types == null)
  331. return null;
  332. else {
  333. IntPtr[] res = new IntPtr [types.Length];
  334. for (int i = 0; i < types.Length; ++i) {
  335. if (types [i] == null)
  336. throw new ArgumentException ();
  337. res [i] = types [i].TypeHandle.Value;
  338. }
  339. return res;
  340. }
  341. }
  342. // This calls ves_icall_reflection_get_token, so needs a Module argument
  343. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  344. internal static extern int get_MetadataToken (Module module);
  345. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  346. internal static extern int GetMDStreamVersion (IntPtr module);
  347. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  348. internal static extern Type[] InternalGetTypes (IntPtr module);
  349. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  350. internal static extern IntPtr GetHINSTANCE (IntPtr module);
  351. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  352. private static extern string GetGuidInternal (IntPtr module);
  353. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  354. internal static extern Type GetGlobalType (IntPtr module);
  355. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  356. internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  357. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  358. internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  359. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  360. internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  361. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  362. internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
  363. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  364. internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  365. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  366. internal static extern byte[] ResolveSignature (IntPtr module, int metadataToken, out ResolveTokenError error);
  367. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  368. internal static extern void GetPEKind (IntPtr module, out PortableExecutableKinds peKind, out ImageFileMachine machine);
  369. }
  370. internal enum ResolveTokenError {
  371. OutOfRange,
  372. BadTable,
  373. Other
  374. }
  375. }