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 {
42 internal enum ResolveTokenError {
49 public class Module : ISerializable, ICustomAttributeProvider {
51 public static readonly TypeFilter FilterTypeName;
52 public static readonly TypeFilter FilterTypeNameIgnoreCase;
54 private IntPtr _impl; /* a pointer to a MonoImage */
55 internal Assembly assembly;
56 internal string fqname;
58 internal string scopename;
59 internal bool is_resource;
62 const BindingFlags defaultBindingFlags =
63 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
66 FilterTypeName = new TypeFilter (filter_by_type_name);
67 FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
70 internal Module () { }
76 public Assembly Assembly {
77 get { return assembly; }
80 public virtual string FullyQualifiedName {
81 get { return fqname; }
88 public string ScopeName {
89 get { return scopename; }
94 public ModuleHandle ModuleHandle {
96 return new ModuleHandle (_impl);
100 public extern int MetadataToken {
101 [MethodImplAttribute (MethodImplOptions.InternalCall)]
106 public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
108 System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
109 Type[] types = GetTypes ();
110 foreach (Type t in types)
111 if (filter (t, filterCriteria))
113 return (Type[])filtered.ToArray (typeof(Type));
116 public virtual object[] GetCustomAttributes(bool inherit)
118 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
121 public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
123 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
126 public FieldInfo GetField (string name)
131 return GetGlobalType ().GetField (name, BindingFlags.Public | BindingFlags.Static);
134 public FieldInfo GetField (string name, BindingFlags flags)
139 return GetGlobalType ().GetField (name, flags);
142 public FieldInfo[] GetFields ()
145 return new FieldInfo [0];
147 return GetGlobalType ().GetFields (BindingFlags.Public | BindingFlags.Static);
150 public MethodInfo GetMethod (string name)
152 return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, Type.EmptyTypes, null);
155 public MethodInfo GetMethod (string name, Type[] types)
157 return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
160 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
162 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
165 protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
170 return GetGlobalType ().GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
173 public MethodInfo[] GetMethods ()
176 return new MethodInfo [0];
178 return GetGlobalType ().GetMethods ();
182 public MethodInfo[] GetMethods (BindingFlags flags) {
184 return new MethodInfo [0];
186 return GetGlobalType ().GetMethods (flags);
189 public FieldInfo[] GetFields (BindingFlags flags)
192 return new FieldInfo [0];
194 return GetGlobalType ().GetFields (flags);
198 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
200 UnitySerializationHolder.GetModuleData (this, info, context);
203 public X509Certificate GetSignerCertificate ()
206 return X509Certificate.CreateFromSignedFile (assembly.Location);
213 public virtual Type GetType(string className)
215 return GetType (className, false, false);
218 public virtual Type GetType(string className, bool ignoreCase)
220 return GetType (className, false, ignoreCase);
223 public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
225 if (className == null)
226 throw new ArgumentNullException ("className");
227 if (className == String.Empty)
228 throw new ArgumentException ("Type name can't be empty");
229 return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
232 [MethodImplAttribute (MethodImplOptions.InternalCall)]
233 private extern Type[] InternalGetTypes ();
235 public virtual Type[] GetTypes()
237 return InternalGetTypes ();
240 public virtual bool IsDefined (Type attributeType, bool inherit)
242 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
245 public bool IsResource()
250 public override string ToString ()
255 #if NET_2_0 || BOOTSTRAP_NET_2_0
262 return Mono_GetGuid (this);
267 private Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
268 if (error == ResolveTokenError.OutOfRange)
269 return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
271 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");
274 public FieldInfo ResolveField (int metadataToken) {
275 ResolveTokenError error;
277 IntPtr handle = ResolveFieldToken (_impl, metadataToken, out error);
278 if (handle == IntPtr.Zero)
279 throw resolve_token_exception (metadataToken, error, "Field");
281 return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
284 public MemberInfo ResolveMember (int metadataToken) {
285 ResolveTokenError error;
287 MemberInfo m = ResolveMemberToken (_impl, metadataToken, out error);
289 throw resolve_token_exception (metadataToken, error, "MemberInfo");
294 public MethodBase ResolveMethod (int metadataToken) {
295 ResolveTokenError error;
297 IntPtr handle = ResolveMethodToken (_impl, metadataToken, out error);
298 if (handle == IntPtr.Zero)
299 throw resolve_token_exception (metadataToken, error, "MethodBase");
301 return MethodBase.GetMethodFromHandle (new RuntimeMethodHandle (handle));
304 public string ResolveString (int metadataToken) {
305 ResolveTokenError error;
307 string s = ResolveStringToken (_impl, metadataToken, out error);
309 throw resolve_token_exception (metadataToken, error, "string");
314 public Type ResolveType (int metadataToken) {
315 ResolveTokenError error;
317 IntPtr handle = ResolveTypeToken (_impl, metadataToken, out error);
318 if (handle == IntPtr.Zero)
319 throw resolve_token_exception (metadataToken, error, "Type");
321 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
325 // Mono Extension: returns the GUID of this module
326 internal static Guid Mono_GetGuid (Module module)
328 return new Guid (module.GetGuidInternal ());
331 private static bool filter_by_type_name (Type m, object filterCriteria) {
332 string s = (string)filterCriteria;
333 if (s.EndsWith ("*"))
334 return m.Name.StartsWith (s.Substring (0, s.Length - 1));
339 private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
340 string s = (string)filterCriteria;
341 if (s.EndsWith ("*"))
342 return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
344 return String.Compare (m.Name, s, true) == 0;
347 [MethodImplAttribute (MethodImplOptions.InternalCall)]
348 private extern string GetGuidInternal ();
350 [MethodImplAttribute (MethodImplOptions.InternalCall)]
351 private extern Type GetGlobalType ();
353 [MethodImplAttribute (MethodImplOptions.InternalCall)]
354 private extern void Close ();
356 [MethodImplAttribute (MethodImplOptions.InternalCall)]
357 internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, out ResolveTokenError error);
359 [MethodImplAttribute (MethodImplOptions.InternalCall)]
360 internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, out ResolveTokenError error);
362 [MethodImplAttribute (MethodImplOptions.InternalCall)]
363 internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, out ResolveTokenError error);
365 [MethodImplAttribute (MethodImplOptions.InternalCall)]
366 internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
368 [MethodImplAttribute (MethodImplOptions.InternalCall)]
369 internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, out ResolveTokenError error);
371 [MethodImplAttribute (MethodImplOptions.InternalCall)]
372 internal static extern void GetPEKind (IntPtr module, out PortableExecutableKind peKind, out ImageFileMachine machine);