Module.cs 12 KB

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