MonoModule.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // System.Reflection/MonoModule.cs
  3. //
  4. // Author:
  5. // Rodrigo Kumpera ([email protected])
  6. //
  7. // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Globalization;
  32. using System.Runtime.InteropServices;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.Security;
  35. using System.Security.Permissions;
  36. using System.Runtime.Serialization;
  37. namespace System.Reflection {
  38. abstract class RuntimeModule : Module
  39. {
  40. }
  41. [ComVisible (true)]
  42. [ComDefaultInterfaceAttribute (typeof (_Module))]
  43. [Serializable]
  44. [ClassInterface(ClassInterfaceType.None)]
  45. class MonoModule : RuntimeModule
  46. {
  47. public
  48. override
  49. Assembly Assembly {
  50. get { return assembly; }
  51. }
  52. public
  53. override
  54. // Note: we do not ask for PathDiscovery because no path is returned here.
  55. // However MS Fx requires it (see FDBK23572 for details).
  56. string Name {
  57. get { return name; }
  58. }
  59. public
  60. override
  61. string ScopeName {
  62. get { return scopename; }
  63. }
  64. public
  65. override
  66. int MDStreamVersion {
  67. get {
  68. if (_impl == IntPtr.Zero)
  69. throw new NotSupportedException ();
  70. return GetMDStreamVersion (_impl);
  71. }
  72. }
  73. public
  74. override
  75. Guid ModuleVersionId {
  76. get {
  77. return GetModuleVersionId ();
  78. }
  79. }
  80. public override
  81. string FullyQualifiedName {
  82. get {
  83. #if !NET_2_1
  84. if (SecurityManager.SecurityEnabled) {
  85. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
  86. }
  87. #endif
  88. return fqname;
  89. }
  90. }
  91. public
  92. override
  93. bool IsResource()
  94. {
  95. return is_resource;
  96. }
  97. public override
  98. Type[] FindTypes(TypeFilter filter, object filterCriteria)
  99. {
  100. var filtered = new List<Type> ();
  101. Type[] types = GetTypes ();
  102. foreach (Type t in types)
  103. if (filter (t, filterCriteria))
  104. filtered.Add (t);
  105. return filtered.ToArray ();
  106. }
  107. public override
  108. object[] GetCustomAttributes(bool inherit)
  109. {
  110. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  111. }
  112. public override
  113. object[] GetCustomAttributes(Type attributeType, bool inherit)
  114. {
  115. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  116. }
  117. public override
  118. FieldInfo GetField (string name, BindingFlags bindingAttr)
  119. {
  120. if (name == null)
  121. throw new ArgumentNullException("name");
  122. if (IsResource ())
  123. return null;
  124. Type globalType = GetGlobalType ();
  125. return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
  126. }
  127. public override
  128. FieldInfo[] GetFields (BindingFlags bindingFlags)
  129. {
  130. if (IsResource ())
  131. return new FieldInfo [0];
  132. Type globalType = GetGlobalType ();
  133. return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
  134. }
  135. public override
  136. int MetadataToken {
  137. get { return get_MetadataToken (this); }
  138. }
  139. protected
  140. override
  141. MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  142. {
  143. if (IsResource ())
  144. return null;
  145. Type globalType = GetGlobalType ();
  146. if (globalType == null)
  147. return null;
  148. if (types == null)
  149. return globalType.GetMethod (name);
  150. return globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
  151. }
  152. public
  153. override
  154. MethodInfo[] GetMethods (BindingFlags bindingFlags) {
  155. if (IsResource ())
  156. return new MethodInfo [0];
  157. Type globalType = GetGlobalType ();
  158. return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
  159. }
  160. public override
  161. void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
  162. ModuleHandle.GetPEKind (out peKind, out machine);
  163. }
  164. public override
  165. Type GetType(string className, bool throwOnError, bool ignoreCase)
  166. {
  167. if (className == null)
  168. throw new ArgumentNullException ("className");
  169. if (className == String.Empty)
  170. throw new ArgumentException ("Type name can't be empty");
  171. return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
  172. }
  173. public override
  174. bool IsDefined (Type attributeType, bool inherit)
  175. {
  176. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  177. }
  178. public
  179. override
  180. FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  181. ResolveTokenError error;
  182. IntPtr handle = ResolveFieldToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  183. if (handle == IntPtr.Zero)
  184. throw resolve_token_exception (metadataToken, error, "Field");
  185. else
  186. return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
  187. }
  188. public
  189. override
  190. MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  191. ResolveTokenError error;
  192. MemberInfo m = ResolveMemberToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  193. if (m == null)
  194. throw resolve_token_exception (metadataToken, error, "MemberInfo");
  195. else
  196. return m;
  197. }
  198. public
  199. override
  200. MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  201. ResolveTokenError error;
  202. IntPtr handle = ResolveMethodToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  203. if (handle == IntPtr.Zero)
  204. throw resolve_token_exception (metadataToken, error, "MethodBase");
  205. else
  206. return MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
  207. }
  208. public
  209. override
  210. string ResolveString (int metadataToken) {
  211. ResolveTokenError error;
  212. string s = ResolveStringToken (_impl, metadataToken, out error);
  213. if (s == null)
  214. throw resolve_token_exception (metadataToken, error, "string");
  215. else
  216. return s;
  217. }
  218. public
  219. override
  220. Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
  221. ResolveTokenError error;
  222. IntPtr handle = ResolveTypeToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
  223. if (handle == IntPtr.Zero)
  224. throw resolve_token_exception (metadataToken, error, "Type");
  225. else
  226. return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
  227. }
  228. public
  229. override
  230. byte[] ResolveSignature (int metadataToken) {
  231. ResolveTokenError error;
  232. byte[] res = ResolveSignature (_impl, metadataToken, out error);
  233. if (res == null)
  234. throw resolve_token_exception (metadataToken, error, "signature");
  235. else
  236. return res;
  237. }
  238. public override void GetObjectData (SerializationInfo info, StreamingContext context)
  239. {
  240. if (info == null)
  241. throw new ArgumentNullException ("info");
  242. UnitySerializationHolder.GetUnitySerializationInfo (info, UnitySerializationHolder.ModuleUnity, this.ScopeName, this.GetRuntimeAssembly ());
  243. }
  244. #if !NET_2_1
  245. public
  246. override
  247. X509Certificate GetSignerCertificate ()
  248. {
  249. try {
  250. return X509Certificate.CreateFromSignedFile (assembly.Location);
  251. }
  252. catch {
  253. return null;
  254. }
  255. }
  256. #endif
  257. public override
  258. Type[] GetTypes()
  259. {
  260. return InternalGetTypes ();
  261. }
  262. public override IList<CustomAttributeData> GetCustomAttributesData () {
  263. return CustomAttributeData.GetCustomAttributes (this);
  264. }
  265. internal RuntimeAssembly GetRuntimeAssembly ()
  266. {
  267. return (RuntimeAssembly)assembly;
  268. }
  269. }
  270. }