Module.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. internal Module () { }
  51. ~Module () {
  52. Close ();
  53. }
  54. public Assembly Assembly {
  55. get { return assembly; }
  56. }
  57. public virtual string FullyQualifiedName {
  58. get { return fqname; }
  59. }
  60. public string Name {
  61. get { return name; }
  62. }
  63. public string ScopeName {
  64. get { return scopename; }
  65. }
  66. public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
  67. {
  68. System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
  69. Type[] types = GetTypes ();
  70. foreach (Type t in types)
  71. if (filter (t, filterCriteria))
  72. filtered.Add (t);
  73. return (Type[])filtered.ToArray (typeof(Type));
  74. }
  75. public virtual object[] GetCustomAttributes(bool inherit)
  76. {
  77. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  78. }
  79. public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
  80. {
  81. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  82. }
  83. public FieldInfo GetField (string name)
  84. {
  85. if (IsResource ())
  86. return null;
  87. return GetGlobalType ().GetField (name, BindingFlags.Public | BindingFlags.Static);
  88. }
  89. public FieldInfo GetField (string name, BindingFlags flags)
  90. {
  91. if (IsResource ())
  92. return null;
  93. return GetGlobalType ().GetField (name, flags);
  94. }
  95. public FieldInfo[] GetFields ()
  96. {
  97. if (IsResource ())
  98. return new FieldInfo [0];
  99. return GetGlobalType ().GetFields (BindingFlags.Public | BindingFlags.Static);
  100. }
  101. public MethodInfo GetMethod (string name)
  102. {
  103. return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, Type.EmptyTypes, null);
  104. }
  105. public MethodInfo GetMethod (string name, Type[] types)
  106. {
  107. return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
  108. }
  109. public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  110. {
  111. return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
  112. }
  113. protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  114. {
  115. if (IsResource ())
  116. return null;
  117. return GetGlobalType ().GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
  118. }
  119. public MethodInfo[] GetMethods ()
  120. {
  121. if (IsResource ())
  122. return new MethodInfo [0];
  123. return GetGlobalType ().GetMethods ();
  124. }
  125. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  126. {
  127. UnitySerializationHolder.GetModuleData (this, info, context);
  128. }
  129. public X509Certificate GetSignerCertificate ()
  130. {
  131. try {
  132. return X509Certificate.CreateFromSignedFile (assembly.Location);
  133. }
  134. catch {
  135. return null;
  136. }
  137. }
  138. public virtual Type GetType(string className)
  139. {
  140. return GetType (className, false, false);
  141. }
  142. public virtual Type GetType(string className, bool ignoreCase)
  143. {
  144. return GetType (className, false, ignoreCase);
  145. }
  146. public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
  147. {
  148. if (className == null)
  149. throw new ArgumentNullException ("className");
  150. if (className == String.Empty)
  151. throw new ArgumentException ("Type name can't be empty");
  152. return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
  153. }
  154. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  155. private extern Type[] InternalGetTypes ();
  156. public virtual Type[] GetTypes()
  157. {
  158. return InternalGetTypes ();
  159. }
  160. public virtual bool IsDefined (Type attributeType, bool inherit)
  161. {
  162. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  163. }
  164. public bool IsResource()
  165. {
  166. return is_resource;
  167. }
  168. public override string ToString ()
  169. {
  170. return "Reflection.Module: " + name;
  171. }
  172. // Mono Extension: returns the GUID of this module
  173. internal Guid Mono_GetGuid ()
  174. {
  175. return new Guid (GetGuidInternal ());
  176. }
  177. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  178. private extern string GetGuidInternal ();
  179. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  180. private extern Type GetGlobalType ();
  181. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  182. private extern void Close ();
  183. }
  184. }