2
0

Module.cs 13 KB

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