2 // System.Reflection/Module.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Reflection;
35 using System.Runtime.Serialization;
36 using System.Security.Cryptography.X509Certificates;
37 using System.Runtime.InteropServices;
38 using System.Runtime.CompilerServices;
40 namespace System.Reflection {
43 public class Module : ISerializable, ICustomAttributeProvider {
45 public static readonly TypeFilter FilterTypeName;
46 public static readonly TypeFilter FilterTypeNameIgnoreCase;
48 private IntPtr _impl; /* a pointer to a MonoImage */
49 internal Assembly assembly;
50 internal string fqname;
52 internal string scopename;
53 internal bool is_resource;
55 const BindingFlags defaultBindingFlags =
56 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
59 FilterTypeName = new TypeFilter (filter_by_type_name);
60 FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
63 internal Module () { }
69 public Assembly Assembly {
70 get { return assembly; }
73 public virtual string FullyQualifiedName {
74 get { return fqname; }
81 public string ScopeName {
82 get { return scopename; }
85 public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
87 System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
88 Type[] types = GetTypes ();
89 foreach (Type t in types)
90 if (filter (t, filterCriteria))
92 return (Type[])filtered.ToArray (typeof(Type));
95 public virtual object[] GetCustomAttributes(bool inherit)
97 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
100 public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
102 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
105 public FieldInfo GetField (string name)
110 return GetGlobalType ().GetField (name, BindingFlags.Public | BindingFlags.Static);
113 public FieldInfo GetField (string name, BindingFlags flags)
118 return GetGlobalType ().GetField (name, flags);
121 public FieldInfo[] GetFields ()
124 return new FieldInfo [0];
126 return GetGlobalType ().GetFields (BindingFlags.Public | BindingFlags.Static);
129 public MethodInfo GetMethod (string name)
131 return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, Type.EmptyTypes, null);
134 public MethodInfo GetMethod (string name, Type[] types)
136 return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
139 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
141 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
144 protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
149 return GetGlobalType ().GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
152 public MethodInfo[] GetMethods ()
155 return new MethodInfo [0];
157 return GetGlobalType ().GetMethods ();
160 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
162 UnitySerializationHolder.GetModuleData (this, info, context);
165 public X509Certificate GetSignerCertificate ()
168 return X509Certificate.CreateFromSignedFile (assembly.Location);
175 public virtual Type GetType(string className)
177 return GetType (className, false, false);
180 public virtual Type GetType(string className, bool ignoreCase)
182 return GetType (className, false, ignoreCase);
185 public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
187 if (className == null)
188 throw new ArgumentNullException ("className");
189 if (className == String.Empty)
190 throw new ArgumentException ("Type name can't be empty");
191 return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
194 [MethodImplAttribute (MethodImplOptions.InternalCall)]
195 private extern Type[] InternalGetTypes ();
197 public virtual Type[] GetTypes()
199 return InternalGetTypes ();
202 public virtual bool IsDefined (Type attributeType, bool inherit)
204 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
207 public bool IsResource()
212 public override string ToString ()
217 // Mono Extension: returns the GUID of this module
218 internal Guid Mono_GetGuid ()
220 return new Guid (GetGuidInternal ());
223 private static bool filter_by_type_name (Type m, object filterCriteria) {
224 string s = (string)filterCriteria;
225 if (s.EndsWith ("*"))
226 return m.Name.StartsWith (s.Substring (0, s.Length - 1));
231 private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
232 string s = (string)filterCriteria;
233 if (s.EndsWith ("*"))
234 return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
236 return String.Compare (m.Name, s, true) == 0;
239 [MethodImplAttribute (MethodImplOptions.InternalCall)]
240 private extern string GetGuidInternal ();
242 [MethodImplAttribute (MethodImplOptions.InternalCall)]
243 private extern Type GetGlobalType ();
245 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246 private extern void Close ();