Module.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. namespace System.Reflection {
  36. internal enum ResolveTokenError {
  37. OutOfRange,
  38. BadTable,
  39. Other
  40. };
  41. #if NET_2_0
  42. [ComVisible (true)]
  43. [ComDefaultInterfaceAttribute (typeof (_Module))]
  44. #endif
  45. [Serializable]
  46. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  47. public class Module : ISerializable, ICustomAttributeProvider, _Module {
  48. public static readonly TypeFilter FilterTypeName;
  49. public static readonly TypeFilter FilterTypeNameIgnoreCase;
  50. #pragma warning disable 649
  51. private 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. #pragma warning restore 649
  59. const BindingFlags defaultBindingFlags =
  60. BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
  61. static Module () {
  62. FilterTypeName = new TypeFilter (filter_by_type_name);
  63. FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
  64. }
  65. internal Module () {
  66. }
  67. public Assembly Assembly {
  68. get { return assembly; }
  69. }
  70. public virtual string FullyQualifiedName {
  71. get {
  72. if (SecurityManager.SecurityEnabled) {
  73. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
  74. }
  75. return fqname;
  76. }
  77. }
  78. // Note: we do not ask for PathDiscovery because no path is returned here.
  79. // However MS Fx requires it (see FDBK23572 for details).
  80. public string Name {
  81. get { return name; }
  82. }
  83. public string ScopeName {
  84. get { return scopename; }
  85. }
  86. #if NET_2_0
  87. public ModuleHandle ModuleHandle {
  88. get {
  89. return new ModuleHandle (_impl);
  90. }
  91. }
  92. public extern int MetadataToken {
  93. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  94. get;
  95. }
  96. public int MDStreamVersion {
  97. get {
  98. if (_impl == IntPtr.Zero)
  99. throw new NotSupportedException ();
  100. return GetMDStreamVersion (_impl);
  101. }
  102. }
  103. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  104. internal static extern int GetMDStreamVersion (IntPtr module_handle);
  105. #endif
  106. public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
  107. {
  108. System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
  109. Type[] types = GetTypes ();
  110. foreach (Type t in types)
  111. if (filter (t, filterCriteria))
  112. filtered.Add (t);
  113. return (Type[])filtered.ToArray (typeof(Type));
  114. }
  115. public virtual object[] GetCustomAttributes(bool inherit)
  116. {
  117. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  118. }
  119. public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
  120. {
  121. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  122. }
  123. public FieldInfo GetField (string name)
  124. {
  125. if (IsResource ())
  126. return null;
  127. Type globalType = GetGlobalType ();
  128. return (globalType != null) ? globalType.GetField (name, BindingFlags.Public | BindingFlags.Static) : null;
  129. }
  130. public FieldInfo GetField (string name, BindingFlags bindingAttr)
  131. {
  132. if (IsResource ())
  133. return null;
  134. Type globalType = GetGlobalType ();
  135. return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
  136. }
  137. public FieldInfo[] GetFields ()
  138. {
  139. if (IsResource ())
  140. return new FieldInfo [0];
  141. Type globalType = GetGlobalType ();
  142. return (globalType != null) ? globalType.GetFields (BindingFlags.Public | BindingFlags.Static) : new FieldInfo [0];
  143. }
  144. public MethodInfo GetMethod (string name)
  145. {
  146. // Can't call the other overloads since they call Type.GetMethod () which does a null check on the 'types' array
  147. if (IsResource ())
  148. return null;
  149. Type globalType = GetGlobalType ();
  150. return (globalType != null) ? globalType.GetMethod (name) : null;
  151. }
  152. public MethodInfo GetMethod (string name, Type[] types)
  153. {
  154. return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
  155. }
  156. public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  157. {
  158. return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
  159. }
  160. protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  161. {
  162. if (IsResource ())
  163. return null;
  164. Type globalType = GetGlobalType ();
  165. return (globalType != null) ? globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers) : null;
  166. }
  167. public MethodInfo[] GetMethods ()
  168. {
  169. if (IsResource ())
  170. return new MethodInfo [0];
  171. Type globalType = GetGlobalType ();
  172. return (globalType != null) ? globalType.GetMethods () : new MethodInfo [0];
  173. }
  174. #if NET_2_0
  175. public MethodInfo[] GetMethods (BindingFlags bindingFlags) {
  176. if (IsResource ())
  177. return new MethodInfo [0];
  178. Type globalType = GetGlobalType ();
  179. return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
  180. }
  181. public FieldInfo[] GetFields (BindingFlags bindingFlags)
  182. {
  183. if (IsResource ())
  184. return new FieldInfo [0];
  185. Type globalType = GetGlobalType ();
  186. return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
  187. }
  188. #endif
  189. [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
  190. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  191. {
  192. if (info == null)
  193. throw new ArgumentNullException ("info");
  194. UnitySerializationHolder.GetModuleData (this, info, context);
  195. }
  196. public X509Certificate GetSignerCertificate ()
  197. {
  198. try {
  199. return X509Certificate.CreateFromSignedFile (assembly.Location);
  200. }
  201. catch {
  202. return null;
  203. }
  204. }
  205. #if NET_2_0
  206. [ComVisible (true)]
  207. #endif
  208. public virtual Type GetType(string className)
  209. {
  210. return GetType (className, false, false);
  211. }
  212. #if NET_2_0
  213. [ComVisible (true)]
  214. #endif
  215. public virtual Type GetType(string className, bool ignoreCase)
  216. {
  217. return GetType (className, false, ignoreCase);
  218. }
  219. #if NET_2_0
  220. [ComVisible (true)]
  221. #endif
  222. public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
  223. {
  224. if (className == null)
  225. throw new ArgumentNullException ("className");
  226. if (className == String.Empty)
  227. throw new ArgumentException ("Type name can't be empty");
  228. return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
  229. }
  230. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  231. private extern Type[] InternalGetTypes ();
  232. public virtual Type[] GetTypes()
  233. {
  234. return InternalGetTypes ();
  235. }
  236. public virtual bool IsDefined (Type attributeType, bool inherit)
  237. {
  238. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  239. }
  240. public bool IsResource()
  241. {
  242. return is_resource;
  243. }
  244. public override string ToString ()
  245. {
  246. return name;
  247. }
  248. internal Guid MvId {
  249. get {
  250. return Mono_GetGuid (this);
  251. }
  252. }
  253. #if NET_2_0
  254. public Guid ModuleVersionId {
  255. get {
  256. return Mono_GetGuid (this);
  257. }
  258. }
  259. public void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
  260. ModuleHandle.GetPEKind (out peKind, out machine);
  261. }
  262. #endif
  263. #if NET_2_0
  264. private Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
  265. if (error == ResolveTokenError.OutOfRange)
  266. return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
  267. else
  268. 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");
  269. }
  270. private IntPtr[] ptrs_from_types (Type[] types) {
  271. if (types == null)
  272. return null;
  273. else {
  274. IntPtr[] res = new IntPtr [types.Length];
  275. for (int i = 0; i < types.Length; ++i) {
  276. if (types [i] == null)
  277. throw new ArgumentException ();
  278. res [i] = types [i].TypeHandle.Value;
  279. }
  280. return res;
  281. }
  282. }
  283. public FieldInfo ResolveField (int metadataToken) {
  284. return ResolveField (metadataToken, null, null);
  285. }
  286. public FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  287. ResolveTokenError error;
  288. IntPtr handle = ResolveFieldToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  289. if (handle == IntPtr.Zero)
  290. throw resolve_token_exception (metadataToken, error, "Field");
  291. else
  292. return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
  293. }
  294. public MemberInfo ResolveMember (int metadataToken) {
  295. return ResolveMember (metadataToken, null, null);
  296. }
  297. public MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  298. ResolveTokenError error;
  299. MemberInfo m = ResolveMemberToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  300. if (m == null)
  301. throw resolve_token_exception (metadataToken, error, "MemberInfo");
  302. else
  303. return m;
  304. }
  305. public MethodBase ResolveMethod (int metadataToken) {
  306. return ResolveMethod (metadataToken, null, null);
  307. }
  308. public MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  309. ResolveTokenError error;
  310. IntPtr handle = ResolveMethodToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  311. if (handle == IntPtr.Zero)
  312. throw resolve_token_exception (metadataToken, error, "MethodBase");
  313. else
  314. return MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
  315. }
  316. public string ResolveString (int metadataToken) {
  317. ResolveTokenError error;
  318. string s = ResolveStringToken (_impl, metadataToken, out error);
  319. if (s == null)
  320. throw resolve_token_exception (metadataToken, error, "string");
  321. else
  322. return s;
  323. }
  324. public Type ResolveType (int metadataToken) {
  325. return ResolveType (metadataToken, null, null);
  326. }
  327. public Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  328. ResolveTokenError error;
  329. IntPtr handle = ResolveTypeToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  330. if (handle == IntPtr.Zero)
  331. throw resolve_token_exception (metadataToken, error, "Type");
  332. else
  333. return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
  334. }
  335. public byte[] ResolveSignature (int metadataToken) {
  336. ResolveTokenError error;
  337. byte[] res = ResolveSignature (_impl, metadataToken, out error);
  338. if (res == null)
  339. throw resolve_token_exception (metadataToken, error, "signature");
  340. else
  341. return res;
  342. }
  343. #endif
  344. internal static Type MonoDebugger_ResolveType (Module module, int token)
  345. {
  346. ResolveTokenError error;
  347. IntPtr handle = ResolveTypeToken (module._impl, token, null, null, out error);
  348. if (handle == IntPtr.Zero)
  349. return null;
  350. else
  351. return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
  352. }
  353. // Mono Extension: returns the GUID of this module
  354. internal static Guid Mono_GetGuid (Module module)
  355. {
  356. return new Guid (module.GetGuidInternal ());
  357. }
  358. private static bool filter_by_type_name (Type m, object filterCriteria) {
  359. string s = (string)filterCriteria;
  360. if (s.EndsWith ("*"))
  361. return m.Name.StartsWith (s.Substring (0, s.Length - 1));
  362. else
  363. return m.Name == s;
  364. }
  365. private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
  366. string s = (string)filterCriteria;
  367. if (s.EndsWith ("*"))
  368. return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
  369. else
  370. return String.Compare (m.Name, s, true) == 0;
  371. }
  372. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  373. internal extern IntPtr GetHINSTANCE ();
  374. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  375. private extern string GetGuidInternal ();
  376. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  377. private extern Type GetGlobalType ();
  378. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  379. internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  380. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  381. internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  382. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  383. internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  384. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  385. internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
  386. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  387. internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  388. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  389. internal static extern byte[] ResolveSignature (IntPtr module, int metadataToken, out ResolveTokenError error);
  390. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  391. internal static extern void GetPEKind (IntPtr module, out PortableExecutableKinds peKind, out ImageFileMachine machine);
  392. #if NET_1_1
  393. void _Module.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  394. {
  395. throw new NotImplementedException ();
  396. }
  397. void _Module.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  398. {
  399. throw new NotImplementedException ();
  400. }
  401. void _Module.GetTypeInfoCount (out uint pcTInfo)
  402. {
  403. throw new NotImplementedException ();
  404. }
  405. void _Module.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
  406. IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  407. {
  408. throw new NotImplementedException ();
  409. }
  410. #endif
  411. }
  412. }