Module.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Reflection;
  33. using System.Runtime.Serialization;
  34. using System.Security.Cryptography.X509Certificates;
  35. using System.Runtime.InteropServices;
  36. using System.Runtime.CompilerServices;
  37. namespace System.Reflection {
  38. [Serializable]
  39. public class Module : ISerializable, ICustomAttributeProvider {
  40. public static readonly TypeFilter FilterTypeName;
  41. public static readonly TypeFilter FilterTypeNameIgnoreCase;
  42. private IntPtr _impl; /* a pointer to a MonoImage */
  43. internal Assembly assembly;
  44. internal string fqname;
  45. internal string name;
  46. internal string scopename;
  47. internal bool is_resource;
  48. const BindingFlags defaultBindingFlags =
  49. BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
  50. static Module () {
  51. FilterTypeName = new TypeFilter (filter_by_type_name);
  52. FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
  53. }
  54. internal Module () { }
  55. ~Module () {
  56. Close ();
  57. }
  58. public Assembly Assembly {
  59. get { return assembly; }
  60. }
  61. public virtual string FullyQualifiedName {
  62. get { return fqname; }
  63. }
  64. public string Name {
  65. get { return name; }
  66. }
  67. public string ScopeName {
  68. get { return scopename; }
  69. }
  70. public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
  71. {
  72. System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
  73. Type[] types = GetTypes ();
  74. foreach (Type t in types)
  75. if (filter (t, filterCriteria))
  76. filtered.Add (t);
  77. return (Type[])filtered.ToArray (typeof(Type));
  78. }
  79. public virtual object[] GetCustomAttributes(bool inherit)
  80. {
  81. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  82. }
  83. public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
  84. {
  85. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  86. }
  87. public FieldInfo GetField (string name)
  88. {
  89. if (IsResource ())
  90. return null;
  91. return GetGlobalType ().GetField (name, BindingFlags.Public | BindingFlags.Static);
  92. }
  93. public FieldInfo GetField (string name, BindingFlags flags)
  94. {
  95. if (IsResource ())
  96. return null;
  97. return GetGlobalType ().GetField (name, flags);
  98. }
  99. public FieldInfo[] GetFields ()
  100. {
  101. if (IsResource ())
  102. return new FieldInfo [0];
  103. return GetGlobalType ().GetFields (BindingFlags.Public | BindingFlags.Static);
  104. }
  105. public MethodInfo GetMethod (string name)
  106. {
  107. return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, Type.EmptyTypes, null);
  108. }
  109. public MethodInfo GetMethod (string name, Type[] types)
  110. {
  111. return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
  112. }
  113. public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  114. {
  115. return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
  116. }
  117. protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  118. {
  119. if (IsResource ())
  120. return null;
  121. return GetGlobalType ().GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
  122. }
  123. public MethodInfo[] GetMethods ()
  124. {
  125. if (IsResource ())
  126. return new MethodInfo [0];
  127. return GetGlobalType ().GetMethods ();
  128. }
  129. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  130. {
  131. UnitySerializationHolder.GetModuleData (this, info, context);
  132. }
  133. public X509Certificate GetSignerCertificate ()
  134. {
  135. try {
  136. return X509Certificate.CreateFromSignedFile (assembly.Location);
  137. }
  138. catch {
  139. return null;
  140. }
  141. }
  142. public virtual Type GetType(string className)
  143. {
  144. return GetType (className, false, false);
  145. }
  146. public virtual Type GetType(string className, bool ignoreCase)
  147. {
  148. return GetType (className, false, ignoreCase);
  149. }
  150. public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
  151. {
  152. if (className == null)
  153. throw new ArgumentNullException ("className");
  154. if (className == String.Empty)
  155. throw new ArgumentException ("Type name can't be empty");
  156. return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
  157. }
  158. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  159. private extern Type[] InternalGetTypes ();
  160. public virtual Type[] GetTypes()
  161. {
  162. return InternalGetTypes ();
  163. }
  164. public virtual bool IsDefined (Type attributeType, bool inherit)
  165. {
  166. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  167. }
  168. public bool IsResource()
  169. {
  170. return is_resource;
  171. }
  172. public override string ToString ()
  173. {
  174. return name;
  175. }
  176. #if NET_2_0 || BOOTSTRAP_NET_2_0
  177. public
  178. #else
  179. internal
  180. #endif
  181. Guid Mvid {
  182. get {
  183. return Mono_GetGuid (this);
  184. }
  185. }
  186. // Mono Extension: returns the GUID of this module
  187. internal static Guid Mono_GetGuid (Module module)
  188. {
  189. return new Guid (module.GetGuidInternal ());
  190. }
  191. private static bool filter_by_type_name (Type m, object filterCriteria) {
  192. string s = (string)filterCriteria;
  193. if (s.EndsWith ("*"))
  194. return m.Name.StartsWith (s.Substring (0, s.Length - 1));
  195. else
  196. return m.Name == s;
  197. }
  198. private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
  199. string s = (string)filterCriteria;
  200. if (s.EndsWith ("*"))
  201. return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
  202. else
  203. return String.Compare (m.Name, s, true) == 0;
  204. }
  205. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  206. private extern string GetGuidInternal ();
  207. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  208. private extern Type GetGlobalType ();
  209. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  210. private extern void Close ();
  211. }
  212. }