MonoModule.cs 9.2 KB

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