Module.cs 11 KB

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