Module.cs 12 KB

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