Module.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  147. {
  148. UnitySerializationHolder.GetModuleData (this, info, context);
  149. }
  150. public X509Certificate GetSignerCertificate ()
  151. {
  152. try {
  153. return X509Certificate.CreateFromSignedFile (assembly.Location);
  154. }
  155. catch {
  156. return null;
  157. }
  158. }
  159. public virtual Type GetType(string className)
  160. {
  161. return GetType (className, false, false);
  162. }
  163. public virtual Type GetType(string className, bool ignoreCase)
  164. {
  165. return GetType (className, false, ignoreCase);
  166. }
  167. public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
  168. {
  169. if (className == null)
  170. throw new ArgumentNullException ("className");
  171. if (className == String.Empty)
  172. throw new ArgumentException ("Type name can't be empty");
  173. return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
  174. }
  175. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  176. private extern Type[] InternalGetTypes ();
  177. public virtual Type[] GetTypes()
  178. {
  179. return InternalGetTypes ();
  180. }
  181. public virtual bool IsDefined (Type attributeType, bool inherit)
  182. {
  183. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  184. }
  185. public bool IsResource()
  186. {
  187. return is_resource;
  188. }
  189. public override string ToString ()
  190. {
  191. return name;
  192. }
  193. #if NET_2_0 || BOOTSTRAP_NET_2_0
  194. public
  195. #else
  196. internal
  197. #endif
  198. Guid Mvid {
  199. get {
  200. return Mono_GetGuid (this);
  201. }
  202. }
  203. #if NET_2_0
  204. private Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
  205. if (error == ResolveTokenError.OutOfRange)
  206. return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
  207. else
  208. 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");
  209. }
  210. public FieldInfo ResolveField (int metadataToken) {
  211. ResolveTokenError error;
  212. IntPtr handle = ResolveFieldToken (_impl, metadataToken, out error);
  213. if (handle == IntPtr.Zero)
  214. throw resolve_token_exception (metadataToken, error, "Field");
  215. else
  216. return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
  217. }
  218. [MonoTODO]
  219. public MemberInfo ResolveMember (int metadataToken) {
  220. ResolveTokenError error;
  221. MemberInfo m = ResolveMemberToken (_impl, metadataToken, out error);
  222. if (m == null)
  223. throw resolve_token_exception (metadataToken, error, "MemberInfo");
  224. else
  225. return m;
  226. }
  227. public MethodBase ResolveMethod (int metadataToken) {
  228. ResolveTokenError error;
  229. IntPtr handle = ResolveMethodToken (_impl, metadataToken, out error);
  230. if (handle == IntPtr.Zero)
  231. throw resolve_token_exception (metadataToken, error, "MethodBase");
  232. else
  233. return MethodBase.GetMethodFromHandle (new RuntimeMethodHandle (handle));
  234. }
  235. public string ResolveString (int metadataToken) {
  236. ResolveTokenError error;
  237. string s = ResolveStringToken (_impl, metadataToken, out error);
  238. if (s == null)
  239. throw resolve_token_exception (metadataToken, error, "string");
  240. else
  241. return s;
  242. }
  243. public Type ResolveType (int metadataToken) {
  244. ResolveTokenError error;
  245. IntPtr handle = ResolveTypeToken (_impl, metadataToken, out error);
  246. if (handle == IntPtr.Zero)
  247. throw resolve_token_exception (metadataToken, error, "Type");
  248. else
  249. return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
  250. }
  251. #endif
  252. // Mono Extension: returns the GUID of this module
  253. internal static Guid Mono_GetGuid (Module module)
  254. {
  255. return new Guid (module.GetGuidInternal ());
  256. }
  257. private static bool filter_by_type_name (Type m, object filterCriteria) {
  258. string s = (string)filterCriteria;
  259. if (s.EndsWith ("*"))
  260. return m.Name.StartsWith (s.Substring (0, s.Length - 1));
  261. else
  262. return m.Name == s;
  263. }
  264. private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
  265. string s = (string)filterCriteria;
  266. if (s.EndsWith ("*"))
  267. return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
  268. else
  269. return String.Compare (m.Name, s, true) == 0;
  270. }
  271. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  272. private extern string GetGuidInternal ();
  273. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  274. private extern Type GetGlobalType ();
  275. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  276. private extern void Close ();
  277. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  278. internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, out ResolveTokenError error);
  279. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  280. internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, out ResolveTokenError error);
  281. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  282. internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, out ResolveTokenError error);
  283. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  284. internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
  285. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  286. internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, out ResolveTokenError error);
  287. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  288. internal static extern void GetPEKind (IntPtr module, out PortableExecutableKind peKind, out ImageFileMachine machine);
  289. }
  290. }