Module.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. using System.Collections.Generic;
  36. namespace System.Reflection {
  37. internal enum ResolveTokenError {
  38. OutOfRange,
  39. BadTable,
  40. Other
  41. };
  42. [ComVisible (true)]
  43. [ComDefaultInterfaceAttribute (typeof (_Module))]
  44. [Serializable]
  45. [ClassInterfaceAttribute (ClassInterfaceType.None)]
  46. [StructLayout (LayoutKind.Sequential)]
  47. #if NET_4_0 || MOONLIGHT || MOBILE
  48. public abstract class Module : ISerializable, ICustomAttributeProvider, _Module {
  49. #else
  50. public partial class Module : ISerializable, ICustomAttributeProvider, _Module {
  51. #endif
  52. public static readonly TypeFilter FilterTypeName = new TypeFilter (filter_by_type_name);
  53. public static readonly TypeFilter FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
  54. #pragma warning disable 649
  55. internal IntPtr _impl; /* a pointer to a MonoImage */
  56. internal Assembly assembly;
  57. internal string fqname;
  58. internal string name;
  59. internal string scopename;
  60. internal bool is_resource;
  61. internal int token;
  62. #pragma warning restore 649
  63. const BindingFlags defaultBindingFlags =
  64. BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
  65. #if NET_4_0 || MOONLIGHT || MOBILE
  66. protected
  67. #else
  68. internal
  69. #endif
  70. Module () {
  71. }
  72. public ModuleHandle ModuleHandle {
  73. get {
  74. return new ModuleHandle (_impl);
  75. }
  76. }
  77. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  78. internal static extern int get_MetadataToken (Module module);
  79. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  80. internal static extern int GetMDStreamVersion (IntPtr module_handle);
  81. public FieldInfo GetField (string name)
  82. {
  83. return GetField (name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
  84. }
  85. public FieldInfo[] GetFields ()
  86. {
  87. return GetFields (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
  88. }
  89. public MethodInfo GetMethod (string name)
  90. {
  91. return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, null, null);
  92. }
  93. public MethodInfo GetMethod (string name, Type[] types)
  94. {
  95. if (types == null)
  96. throw new ArgumentNullException ("types");
  97. return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
  98. }
  99. public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  100. {
  101. if (types == null)
  102. throw new ArgumentNullException ("types");
  103. return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
  104. }
  105. public MethodInfo[] GetMethods ()
  106. {
  107. return GetMethods (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
  108. }
  109. [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
  110. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  111. {
  112. if (info == null)
  113. throw new ArgumentNullException ("info");
  114. UnitySerializationHolder.GetModuleData (this, info, context);
  115. }
  116. [ComVisible (true)]
  117. public virtual Type GetType(string className)
  118. {
  119. return GetType (className, false, false);
  120. }
  121. [ComVisible (true)]
  122. public virtual Type GetType(string className, bool ignoreCase)
  123. {
  124. return GetType (className, false, ignoreCase);
  125. }
  126. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  127. internal extern Type[] InternalGetTypes ();
  128. public override string ToString ()
  129. {
  130. return name;
  131. }
  132. internal Guid MvId {
  133. get {
  134. return GetModuleVersionId ();
  135. }
  136. }
  137. internal Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
  138. if (error == ResolveTokenError.OutOfRange)
  139. return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
  140. else
  141. 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");
  142. }
  143. internal IntPtr[] ptrs_from_types (Type[] types) {
  144. if (types == null)
  145. return null;
  146. else {
  147. IntPtr[] res = new IntPtr [types.Length];
  148. for (int i = 0; i < types.Length; ++i) {
  149. if (types [i] == null)
  150. throw new ArgumentException ();
  151. res [i] = types [i].TypeHandle.Value;
  152. }
  153. return res;
  154. }
  155. }
  156. public FieldInfo ResolveField (int metadataToken) {
  157. return ResolveField (metadataToken, null, null);
  158. }
  159. public MemberInfo ResolveMember (int metadataToken) {
  160. return ResolveMember (metadataToken, null, null);
  161. }
  162. public MethodBase ResolveMethod (int metadataToken) {
  163. return ResolveMethod (metadataToken, null, null);
  164. }
  165. public Type ResolveType (int metadataToken) {
  166. return ResolveType (metadataToken, null, null);
  167. }
  168. internal static Type MonoDebugger_ResolveType (Module module, int token)
  169. {
  170. ResolveTokenError error;
  171. IntPtr handle = ResolveTypeToken (module._impl, token, null, null, out error);
  172. if (handle == IntPtr.Zero)
  173. return null;
  174. else
  175. return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
  176. }
  177. // Used by mcs, the symbol writer, and mdb through reflection
  178. internal static Guid Mono_GetGuid (Module module)
  179. {
  180. return module.GetModuleVersionId ();
  181. }
  182. internal virtual Guid GetModuleVersionId ()
  183. {
  184. return new Guid (GetGuidInternal ());
  185. }
  186. private static bool filter_by_type_name (Type m, object filterCriteria) {
  187. string s = (string)filterCriteria;
  188. if (s.EndsWith ("*"))
  189. return m.Name.StartsWith (s.Substring (0, s.Length - 1));
  190. else
  191. return m.Name == s;
  192. }
  193. private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
  194. string s = (string)filterCriteria;
  195. if (s.EndsWith ("*"))
  196. return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
  197. else
  198. return String.Compare (m.Name, s, true) == 0;
  199. }
  200. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  201. internal extern IntPtr GetHINSTANCE ();
  202. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  203. private extern string GetGuidInternal ();
  204. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  205. internal extern Type GetGlobalType ();
  206. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  207. internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  208. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  209. internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  210. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  211. internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  212. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  213. internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
  214. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  215. internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
  216. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  217. internal static extern byte[] ResolveSignature (IntPtr module, int metadataToken, out ResolveTokenError error);
  218. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  219. internal static extern void GetPEKind (IntPtr module, out PortableExecutableKinds peKind, out ImageFileMachine machine);
  220. void _Module.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  221. {
  222. throw new NotImplementedException ();
  223. }
  224. void _Module.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  225. {
  226. throw new NotImplementedException ();
  227. }
  228. void _Module.GetTypeInfoCount (out uint pcTInfo)
  229. {
  230. throw new NotImplementedException ();
  231. }
  232. void _Module.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
  233. IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  234. {
  235. throw new NotImplementedException ();
  236. }
  237. #if NET_4_0
  238. public override bool Equals (object o)
  239. {
  240. return o == (object) this;
  241. }
  242. public override int GetHashCode ()
  243. {
  244. return base.GetHashCode ();
  245. }
  246. public static bool operator == (Module left, Module right)
  247. {
  248. if ((object)left == (object)right)
  249. return true;
  250. if ((object)left == null ^ (object)right == null)
  251. return false;
  252. return left.Equals (right);
  253. }
  254. public static bool operator != (Module left, Module right)
  255. {
  256. if ((object)left == (object)right)
  257. return false;
  258. if ((object)left == null ^ (object)right == null)
  259. return true;
  260. return !left.Equals (right);
  261. }
  262. #endif
  263. #if NET_4_0 || MOONLIGHT || MOBILE
  264. public virtual Assembly Assembly {
  265. get { throw CreateNIE (); }
  266. }
  267. public virtual string Name {
  268. get { throw CreateNIE (); }
  269. }
  270. public virtual string ScopeName {
  271. get { throw CreateNIE (); }
  272. }
  273. public virtual int MDStreamVersion {
  274. get { throw CreateNIE (); }
  275. }
  276. public virtual Guid ModuleVersionId {
  277. get { throw CreateNIE (); }
  278. }
  279. public virtual string FullyQualifiedName {
  280. get { throw CreateNIE (); }
  281. }
  282. public virtual int MetadataToken {
  283. get { throw CreateNIE (); }
  284. }
  285. static Exception CreateNIE ()
  286. {
  287. return new NotImplementedException ("Derived classes must implement it");
  288. }
  289. public virtual bool IsResource()
  290. {
  291. throw CreateNIE ();
  292. }
  293. public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
  294. {
  295. throw CreateNIE ();
  296. }
  297. public virtual object[] GetCustomAttributes(bool inherit)
  298. {
  299. throw CreateNIE ();
  300. }
  301. public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
  302. {
  303. throw CreateNIE ();
  304. }
  305. public virtual IList<CustomAttributeData> GetCustomAttributesData ()
  306. {
  307. throw CreateNIE ();
  308. }
  309. public virtual FieldInfo GetField (string name, BindingFlags bindingAttr)
  310. {
  311. throw CreateNIE ();
  312. }
  313. public virtual FieldInfo[] GetFields (BindingFlags bindingFlags)
  314. {
  315. throw CreateNIE ();
  316. }
  317. protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  318. {
  319. throw CreateNIE ();
  320. }
  321. public virtual MethodInfo[] GetMethods (BindingFlags bindingFlags)
  322. {
  323. throw CreateNIE ();
  324. }
  325. public virtual void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine)
  326. {
  327. throw CreateNIE ();
  328. }
  329. [ComVisible (true)]
  330. public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
  331. {
  332. throw CreateNIE ();
  333. }
  334. public virtual bool IsDefined (Type attributeType, bool inherit)
  335. {
  336. throw CreateNIE ();
  337. }
  338. public virtual FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
  339. {
  340. throw CreateNIE ();
  341. }
  342. public virtual MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
  343. {
  344. throw CreateNIE ();
  345. }
  346. public virtual MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
  347. {
  348. throw CreateNIE ();
  349. }
  350. public virtual byte[] ResolveSignature (int metadataToken)
  351. {
  352. throw CreateNIE ();
  353. }
  354. public virtual string ResolveString (int metadataToken)
  355. {
  356. throw CreateNIE ();
  357. }
  358. public virtual Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
  359. {
  360. throw CreateNIE ();
  361. }
  362. public virtual X509Certificate GetSignerCertificate ()
  363. {
  364. throw CreateNIE ();
  365. }
  366. public virtual Type[] GetTypes()
  367. {
  368. throw CreateNIE ();
  369. }
  370. #endif
  371. }
  372. }