2 // System.Reflection/Module.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.Serialization;
31 using System.Security.Cryptography.X509Certificates;
32 using System.Runtime.InteropServices;
33 using System.Runtime.CompilerServices;
34 using System.Security;
35 using System.Security.Permissions;
37 namespace System.Reflection {
39 internal enum ResolveTokenError {
46 public class Module : ISerializable, ICustomAttributeProvider {
48 public static readonly TypeFilter FilterTypeName;
49 public static readonly TypeFilter FilterTypeNameIgnoreCase;
51 private IntPtr _impl; /* a pointer to a MonoImage */
52 internal Assembly assembly;
53 internal string fqname;
55 internal string scopename;
56 internal bool is_resource;
59 const BindingFlags defaultBindingFlags =
60 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
63 FilterTypeName = new TypeFilter (filter_by_type_name);
64 FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
70 public Assembly Assembly {
71 get { return assembly; }
74 public virtual string FullyQualifiedName {
76 if (SecurityManager.SecurityEnabled) {
77 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
83 // Note: we do not ask for PathDiscovery because no path is returned here.
84 // However MS Fx requires it (see FDBK23572 for details).
89 public string ScopeName {
90 get { return scopename; }
95 public ModuleHandle ModuleHandle {
97 return new ModuleHandle (_impl);
101 public extern int MetadataToken {
102 [MethodImplAttribute (MethodImplOptions.InternalCall)]
107 public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
109 System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
110 Type[] types = GetTypes ();
111 foreach (Type t in types)
112 if (filter (t, filterCriteria))
114 return (Type[])filtered.ToArray (typeof(Type));
117 public virtual object[] GetCustomAttributes(bool inherit)
119 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
122 public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
124 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
127 public FieldInfo GetField (string name)
132 Type globalType = GetGlobalType ();
133 return (globalType != null) ? globalType.GetField (name, BindingFlags.Public | BindingFlags.Static) : null;
136 public FieldInfo GetField (string name, BindingFlags flags)
141 Type globalType = GetGlobalType ();
142 return (globalType != null) ? globalType.GetField (name, flags) : null;
145 public FieldInfo[] GetFields ()
148 return new FieldInfo [0];
150 Type globalType = GetGlobalType ();
151 return (globalType != null) ? globalType.GetFields (BindingFlags.Public | BindingFlags.Static) : new FieldInfo [0];
154 public MethodInfo GetMethod (string name)
156 return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, Type.EmptyTypes, null);
159 public MethodInfo GetMethod (string name, Type[] types)
161 return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
164 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
166 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
169 protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
174 Type globalType = GetGlobalType ();
175 return (globalType != null) ? globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers) : null;
178 public MethodInfo[] GetMethods ()
181 return new MethodInfo [0];
183 Type globalType = GetGlobalType ();
184 return (globalType != null) ? globalType.GetMethods () : new MethodInfo [0];
188 public MethodInfo[] GetMethods (BindingFlags flags) {
190 return new MethodInfo [0];
192 Type globalType = GetGlobalType ();
193 return (globalType != null) ? globalType.GetMethods (flags) : new MethodInfo [0];
196 public FieldInfo[] GetFields (BindingFlags flags)
199 return new FieldInfo [0];
201 Type globalType = GetGlobalType ();
202 return (globalType != null) ? globalType.GetFields (flags) : new FieldInfo [0];
206 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
207 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
210 throw new ArgumentNullException ("info");
212 UnitySerializationHolder.GetModuleData (this, info, context);
215 public X509Certificate GetSignerCertificate ()
218 return X509Certificate.CreateFromSignedFile (assembly.Location);
225 public virtual Type GetType(string className)
227 return GetType (className, false, false);
230 public virtual Type GetType(string className, bool ignoreCase)
232 return GetType (className, false, ignoreCase);
235 public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
237 if (className == null)
238 throw new ArgumentNullException ("className");
239 if (className == String.Empty)
240 throw new ArgumentException ("Type name can't be empty");
241 return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
244 [MethodImplAttribute (MethodImplOptions.InternalCall)]
245 private extern Type[] InternalGetTypes ();
247 public virtual Type[] GetTypes()
249 return InternalGetTypes ();
252 public virtual bool IsDefined (Type attributeType, bool inherit)
254 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
257 public bool IsResource()
262 public override string ToString ()
267 #if NET_2_0 || BOOTSTRAP_NET_2_0
274 return Mono_GetGuid (this);
279 private Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
280 if (error == ResolveTokenError.OutOfRange)
281 return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
283 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");
286 public FieldInfo ResolveField (int metadataToken) {
287 ResolveTokenError error;
289 IntPtr handle = ResolveFieldToken (_impl, metadataToken, out error);
290 if (handle == IntPtr.Zero)
291 throw resolve_token_exception (metadataToken, error, "Field");
293 return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
296 public MemberInfo ResolveMember (int metadataToken) {
297 ResolveTokenError error;
299 MemberInfo m = ResolveMemberToken (_impl, metadataToken, out error);
301 throw resolve_token_exception (metadataToken, error, "MemberInfo");
306 public MethodBase ResolveMethod (int metadataToken) {
307 ResolveTokenError error;
309 IntPtr handle = ResolveMethodToken (_impl, metadataToken, out error);
310 if (handle == IntPtr.Zero)
311 throw resolve_token_exception (metadataToken, error, "MethodBase");
313 return MethodBase.GetMethodFromHandle (new RuntimeMethodHandle (handle));
316 public string ResolveString (int metadataToken) {
317 ResolveTokenError error;
319 string s = ResolveStringToken (_impl, metadataToken, out error);
321 throw resolve_token_exception (metadataToken, error, "string");
326 public Type ResolveType (int metadataToken) {
327 ResolveTokenError error;
329 IntPtr handle = ResolveTypeToken (_impl, metadataToken, out error);
330 if (handle == IntPtr.Zero)
331 throw resolve_token_exception (metadataToken, error, "Type");
333 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
337 internal static Type MonoDebugger_ResolveType (Module module, int token)
339 ResolveTokenError error;
341 IntPtr handle = ResolveTypeToken (module._impl, token, out error);
342 if (handle == IntPtr.Zero)
345 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
348 // Mono Extension: returns the GUID of this module
349 internal static Guid Mono_GetGuid (Module module)
351 return new Guid (module.GetGuidInternal ());
354 private static bool filter_by_type_name (Type m, object filterCriteria) {
355 string s = (string)filterCriteria;
356 if (s.EndsWith ("*"))
357 return m.Name.StartsWith (s.Substring (0, s.Length - 1));
362 private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
363 string s = (string)filterCriteria;
364 if (s.EndsWith ("*"))
365 return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
367 return String.Compare (m.Name, s, true) == 0;
370 [MethodImplAttribute (MethodImplOptions.InternalCall)]
371 private extern string GetGuidInternal ();
373 [MethodImplAttribute (MethodImplOptions.InternalCall)]
374 private extern Type GetGlobalType ();
376 [MethodImplAttribute (MethodImplOptions.InternalCall)]
377 internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, out ResolveTokenError error);
379 [MethodImplAttribute (MethodImplOptions.InternalCall)]
380 internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, out ResolveTokenError error);
382 [MethodImplAttribute (MethodImplOptions.InternalCall)]
383 internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, out ResolveTokenError error);
385 [MethodImplAttribute (MethodImplOptions.InternalCall)]
386 internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
388 [MethodImplAttribute (MethodImplOptions.InternalCall)]
389 internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, out ResolveTokenError error);
391 [MethodImplAttribute (MethodImplOptions.InternalCall)]
392 internal static extern void GetPEKind (IntPtr module, out PortableExecutableKind peKind, out ImageFileMachine machine);