MonoModule.cs 9.0 KB

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