Module.cs 13 KB

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