Module.cs 13 KB

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