Module.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Reflection;
  33. using System.Runtime.Serialization;
  34. using System.Security.Cryptography.X509Certificates;
  35. using System.Runtime.InteropServices;
  36. using System.Runtime.CompilerServices;
  37. namespace System.Reflection {
  38. internal enum ResolveTokenError {
  39. OutOfRange,
  40. BadTable,
  41. Other
  42. };
  43. [Serializable]
  44. public class Module : ISerializable, ICustomAttributeProvider {
  45. public static readonly TypeFilter FilterTypeName;
  46. public static readonly TypeFilter FilterTypeNameIgnoreCase;
  47. private IntPtr _impl; /* a pointer to a MonoImage */
  48. internal Assembly assembly;
  49. internal string fqname;
  50. internal string name;
  51. internal string scopename;
  52. internal bool is_resource;
  53. internal int token;
  54. const BindingFlags defaultBindingFlags =
  55. BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
  56. static Module () {
  57. FilterTypeName = new TypeFilter (filter_by_type_name);
  58. FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
  59. }
  60. internal Module () {
  61. }
  62. ~Module () {
  63. Close ();
  64. }
  65. public Assembly Assembly {
  66. get { return assembly; }
  67. }
  68. public virtual string FullyQualifiedName {
  69. get { return fqname; }
  70. }
  71. public string Name {
  72. get { return name; }
  73. }
  74. public string ScopeName {
  75. get { return scopename; }
  76. }
  77. #if NET_2_0
  78. [CLSCompliant(false)]
  79. public ModuleHandle ModuleHandle {
  80. get {
  81. return new ModuleHandle (_impl);
  82. }
  83. }
  84. public extern int MetadataToken {
  85. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  86. get;
  87. }
  88. #endif
  89. public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
  90. {
  91. System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
  92. Type[] types = GetTypes ();
  93. foreach (Type t in types)
  94. if (filter (t, filterCriteria))
  95. filtered.Add (t);
  96. return (Type[])filtered.ToArray (typeof(Type));
  97. }
  98. public virtual object[] GetCustomAttributes(bool inherit)
  99. {
  100. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  101. }
  102. public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
  103. {
  104. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  105. }
  106. public FieldInfo GetField (string name)
  107. {
  108. if (IsResource ())
  109. return null;
  110. Type globalType = GetGlobalType ();
  111. return (globalType != null) ? globalType.GetField (name, BindingFlags.Public | BindingFlags.Static) : null;
  112. }
  113. public FieldInfo GetField (string name, BindingFlags flags)
  114. {
  115. if (IsResource ())
  116. return null;
  117. Type globalType = GetGlobalType ();
  118. return (globalType != null) ? globalType.GetField (name, flags) : null;
  119. }
  120. public FieldInfo[] GetFields ()
  121. {
  122. if (IsResource ())
  123. return new FieldInfo [0];
  124. Type globalType = GetGlobalType ();
  125. return (globalType != null) ? globalType.GetFields (BindingFlags.Public | BindingFlags.Static) : new FieldInfo [0];
  126. }
  127. public MethodInfo GetMethod (string name)
  128. {
  129. return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, Type.EmptyTypes, null);
  130. }
  131. public MethodInfo GetMethod (string name, Type[] types)
  132. {
  133. return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
  134. }
  135. public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  136. {
  137. return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
  138. }
  139. protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  140. {
  141. if (IsResource ())
  142. return null;
  143. Type globalType = GetGlobalType ();
  144. return (globalType != null) ? globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers) : null;
  145. }
  146. public MethodInfo[] GetMethods ()
  147. {
  148. if (IsResource ())
  149. return new MethodInfo [0];
  150. Type globalType = GetGlobalType ();
  151. return (globalType != null) ? globalType.GetMethods () : new MethodInfo [0];
  152. }
  153. #if NET_2_0
  154. public MethodInfo[] GetMethods (BindingFlags flags) {
  155. if (IsResource ())
  156. return new MethodInfo [0];
  157. Type globalType = GetGlobalType ();
  158. return (globalType != null) ? globalType.GetMethods (flags) : new MethodInfo [0];
  159. }
  160. public FieldInfo[] GetFields (BindingFlags flags)
  161. {
  162. if (IsResource ())
  163. return new FieldInfo [0];
  164. Type globalType = GetGlobalType ();
  165. return (globalType != null) ? globalType.GetFields (flags) : new FieldInfo [0];
  166. }
  167. #endif
  168. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  169. {
  170. UnitySerializationHolder.GetModuleData (this, info, context);
  171. }
  172. public X509Certificate GetSignerCertificate ()
  173. {
  174. try {
  175. return X509Certificate.CreateFromSignedFile (assembly.Location);
  176. }
  177. catch {
  178. return null;
  179. }
  180. }
  181. public virtual Type GetType(string className)
  182. {
  183. return GetType (className, false, false);
  184. }
  185. public virtual Type GetType(string className, bool ignoreCase)
  186. {
  187. return GetType (className, false, ignoreCase);
  188. }
  189. public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
  190. {
  191. if (className == null)
  192. throw new ArgumentNullException ("className");
  193. if (className == String.Empty)
  194. throw new ArgumentException ("Type name can't be empty");
  195. return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
  196. }
  197. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  198. private extern Type[] InternalGetTypes ();
  199. public virtual Type[] GetTypes()
  200. {
  201. return InternalGetTypes ();
  202. }
  203. public virtual bool IsDefined (Type attributeType, bool inherit)
  204. {
  205. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  206. }
  207. public bool IsResource()
  208. {
  209. return is_resource;
  210. }
  211. public override string ToString ()
  212. {
  213. return name;
  214. }
  215. #if NET_2_0 || BOOTSTRAP_NET_2_0
  216. public
  217. #else
  218. internal
  219. #endif
  220. Guid Mvid {
  221. get {
  222. return Mono_GetGuid (this);
  223. }
  224. }
  225. #if NET_2_0
  226. private Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
  227. if (error == ResolveTokenError.OutOfRange)
  228. return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
  229. else
  230. 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");
  231. }
  232. public FieldInfo ResolveField (int metadataToken) {
  233. ResolveTokenError error;
  234. IntPtr handle = ResolveFieldToken (_impl, metadataToken, out error);
  235. if (handle == IntPtr.Zero)
  236. throw resolve_token_exception (metadataToken, error, "Field");
  237. else
  238. return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
  239. }
  240. public MemberInfo ResolveMember (int metadataToken) {
  241. ResolveTokenError error;
  242. MemberInfo m = ResolveMemberToken (_impl, metadataToken, out error);
  243. if (m == null)
  244. throw resolve_token_exception (metadataToken, error, "MemberInfo");
  245. else
  246. return m;
  247. }
  248. public MethodBase ResolveMethod (int metadataToken) {
  249. ResolveTokenError error;
  250. IntPtr handle = ResolveMethodToken (_impl, metadataToken, out error);
  251. if (handle == IntPtr.Zero)
  252. throw resolve_token_exception (metadataToken, error, "MethodBase");
  253. else
  254. return MethodBase.GetMethodFromHandle (new RuntimeMethodHandle (handle));
  255. }
  256. public string ResolveString (int metadataToken) {
  257. ResolveTokenError error;
  258. string s = ResolveStringToken (_impl, metadataToken, out error);
  259. if (s == null)
  260. throw resolve_token_exception (metadataToken, error, "string");
  261. else
  262. return s;
  263. }
  264. public Type ResolveType (int metadataToken) {
  265. ResolveTokenError error;
  266. IntPtr handle = ResolveTypeToken (_impl, metadataToken, out error);
  267. if (handle == IntPtr.Zero)
  268. throw resolve_token_exception (metadataToken, error, "Type");
  269. else
  270. return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
  271. }
  272. #endif
  273. // Mono Extension: returns the GUID of this module
  274. internal static Guid Mono_GetGuid (Module module)
  275. {
  276. return new Guid (module.GetGuidInternal ());
  277. }
  278. private static bool filter_by_type_name (Type m, object filterCriteria) {
  279. string s = (string)filterCriteria;
  280. if (s.EndsWith ("*"))
  281. return m.Name.StartsWith (s.Substring (0, s.Length - 1));
  282. else
  283. return m.Name == s;
  284. }
  285. private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
  286. string s = (string)filterCriteria;
  287. if (s.EndsWith ("*"))
  288. return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
  289. else
  290. return String.Compare (m.Name, s, true) == 0;
  291. }
  292. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  293. private extern string GetGuidInternal ();
  294. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  295. private extern Type GetGlobalType ();
  296. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  297. private extern void Close ();
  298. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  299. internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, out ResolveTokenError error);
  300. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  301. internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, out ResolveTokenError error);
  302. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  303. internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, out ResolveTokenError error);
  304. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  305. internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
  306. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  307. internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, out ResolveTokenError error);
  308. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  309. internal static extern void GetPEKind (IntPtr module, out PortableExecutableKind peKind, out ImageFileMachine machine);
  310. }
  311. }