Module.cs 12 KB

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