RuntimeModule.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.Security.Cryptography.X509Certificates;
  34. using System.Security;
  35. using System.Security.Permissions;
  36. using System.Runtime.Serialization;
  37. namespace System.Reflection {
  38. [ComVisible (true)]
  39. [ComDefaultInterfaceAttribute (typeof (_Module))]
  40. [Serializable]
  41. [ClassInterface(ClassInterfaceType.None)]
  42. [StructLayout (LayoutKind.Sequential)]
  43. class RuntimeModule : Module
  44. {
  45. #pragma warning disable 649
  46. #region Sync with object-internals.h
  47. #region Sync with ModuleBuilder
  48. internal IntPtr _impl; /* a pointer to a MonoImage */
  49. internal Assembly assembly;
  50. internal string fqname;
  51. internal string name;
  52. internal string scopename;
  53. internal bool is_resource;
  54. internal int token;
  55. #endregion
  56. #endregion
  57. #pragma warning restore 649
  58. public
  59. override
  60. Assembly Assembly {
  61. get { return assembly; }
  62. }
  63. public
  64. override
  65. // Note: we do not ask for PathDiscovery because no path is returned here.
  66. // However MS Fx requires it (see FDBK23572 for details).
  67. string Name {
  68. get { return name; }
  69. }
  70. public
  71. override
  72. string ScopeName {
  73. get { return scopename; }
  74. }
  75. public
  76. override
  77. int MDStreamVersion {
  78. get {
  79. if (_impl == IntPtr.Zero)
  80. throw new NotSupportedException ();
  81. return GetMDStreamVersion (_impl);
  82. }
  83. }
  84. public
  85. override
  86. Guid ModuleVersionId {
  87. get {
  88. return GetModuleVersionId ();
  89. }
  90. }
  91. public override
  92. string FullyQualifiedName {
  93. get {
  94. #if !MOBILE
  95. if (SecurityManager.SecurityEnabled) {
  96. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
  97. }
  98. #endif
  99. return fqname;
  100. }
  101. }
  102. public
  103. override
  104. bool IsResource()
  105. {
  106. return is_resource;
  107. }
  108. public override
  109. Type[] FindTypes(TypeFilter filter, object filterCriteria)
  110. {
  111. var filtered = new List<Type> ();
  112. Type[] types = GetTypes ();
  113. foreach (Type t in types)
  114. if (filter (t, filterCriteria))
  115. filtered.Add (t);
  116. return filtered.ToArray ();
  117. }
  118. public override
  119. object[] GetCustomAttributes(bool inherit)
  120. {
  121. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  122. }
  123. public override
  124. object[] GetCustomAttributes(Type attributeType, bool inherit)
  125. {
  126. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  127. }
  128. public override
  129. FieldInfo GetField (string name, BindingFlags bindingAttr)
  130. {
  131. if (name == null)
  132. throw new ArgumentNullException("name");
  133. if (IsResource ())
  134. return null;
  135. Type globalType = GetGlobalType ();
  136. return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
  137. }
  138. public override
  139. FieldInfo[] GetFields (BindingFlags bindingFlags)
  140. {
  141. if (IsResource ())
  142. return new FieldInfo [0];
  143. Type globalType = GetGlobalType ();
  144. return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
  145. }
  146. public override
  147. int MetadataToken {
  148. get { return get_MetadataToken (this); }
  149. }
  150. protected
  151. override
  152. MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  153. {
  154. if (IsResource ())
  155. return null;
  156. Type globalType = GetGlobalType ();
  157. if (globalType == null)
  158. return null;
  159. if (types == null)
  160. return globalType.GetMethod (name);
  161. return globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
  162. }
  163. public
  164. override
  165. MethodInfo[] GetMethods (BindingFlags bindingFlags) {
  166. if (IsResource ())
  167. return new MethodInfo [0];
  168. Type globalType = GetGlobalType ();
  169. return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
  170. }
  171. internal override ModuleHandle GetModuleHandleImpl() => new ModuleHandle (_impl);
  172. public override
  173. void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
  174. ModuleHandle.GetPEKind (out peKind, out machine);
  175. }
  176. public override
  177. Type GetType(string className, bool throwOnError, bool ignoreCase)
  178. {
  179. if (className == null)
  180. throw new ArgumentNullException ("className");
  181. if (className == String.Empty)
  182. throw new ArgumentException ("Type name can't be empty");
  183. return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
  184. }
  185. public override
  186. bool IsDefined (Type attributeType, bool inherit)
  187. {
  188. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  189. }
  190. public
  191. override
  192. FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  193. ResolveTokenError error;
  194. IntPtr handle = ResolveFieldToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  195. if (handle == IntPtr.Zero)
  196. throw resolve_token_exception (metadataToken, error, "Field");
  197. else
  198. return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
  199. }
  200. public
  201. override
  202. MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  203. ResolveTokenError error;
  204. MemberInfo m = ResolveMemberToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  205. if (m == null)
  206. throw resolve_token_exception (metadataToken, error, "MemberInfo");
  207. else
  208. return m;
  209. }
  210. public
  211. override
  212. MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  213. ResolveTokenError error;
  214. IntPtr handle = ResolveMethodToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  215. if (handle == IntPtr.Zero)
  216. throw resolve_token_exception (metadataToken, error, "MethodBase");
  217. else
  218. return RuntimeMethodInfo.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
  219. }
  220. public
  221. override
  222. string ResolveString (int metadataToken) {
  223. ResolveTokenError error;
  224. string s = ResolveStringToken (_impl, metadataToken, out error);
  225. if (s == null)
  226. throw resolve_token_exception (metadataToken, error, "string");
  227. else
  228. return s;
  229. }
  230. public
  231. override
  232. Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  233. ResolveTokenError error;
  234. IntPtr handle = ResolveTypeToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  235. if (handle == IntPtr.Zero)
  236. throw resolve_token_exception (metadataToken, error, "Type");
  237. else
  238. return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
  239. }
  240. public
  241. override
  242. byte[] ResolveSignature (int metadataToken) {
  243. ResolveTokenError error;
  244. byte[] res = ResolveSignature (_impl, metadataToken, out error);
  245. if (res == null)
  246. throw resolve_token_exception (metadataToken, error, "signature");
  247. else
  248. return res;
  249. }
  250. public override void GetObjectData (SerializationInfo info, StreamingContext context)
  251. {
  252. if (info == null)
  253. throw new ArgumentNullException ("info");
  254. UnitySerializationHolder.GetUnitySerializationInfo (info, UnitySerializationHolder.ModuleUnity, this.ScopeName, this.GetRuntimeAssembly ());
  255. }
  256. #if !MOBILE
  257. public
  258. override
  259. X509Certificate GetSignerCertificate ()
  260. {
  261. try {
  262. return X509Certificate.CreateFromSignedFile (assembly.Location);
  263. }
  264. catch {
  265. return null;
  266. }
  267. }
  268. #endif
  269. public override
  270. Type[] GetTypes()
  271. {
  272. return InternalGetTypes ();
  273. }
  274. public override IList<CustomAttributeData> GetCustomAttributesData () {
  275. return CustomAttributeData.GetCustomAttributes (this);
  276. }
  277. internal RuntimeAssembly GetRuntimeAssembly ()
  278. {
  279. return (RuntimeAssembly)assembly;
  280. }
  281. internal override IntPtr GetImpl () {
  282. return _impl;
  283. }
  284. }
  285. }