Module.cs 13 KB

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