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 {
47 [ComDefaultInterfaceAttribute (typeof (_Module))]
50 [ClassInterfaceAttribute (ClassInterfaceType.None)]
51 public class Module : ISerializable, ICustomAttributeProvider, _Module {
53 public static readonly TypeFilter FilterTypeName;
54 public static readonly TypeFilter FilterTypeNameIgnoreCase;
56 private IntPtr _impl; /* a pointer to a MonoImage */
57 internal Assembly assembly;
58 internal string fqname;
60 internal string scopename;
61 internal bool is_resource;
64 const BindingFlags defaultBindingFlags =
65 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
68 FilterTypeName = new TypeFilter (filter_by_type_name);
69 FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
75 public Assembly Assembly {
76 get { return assembly; }
79 public virtual string FullyQualifiedName {
81 if (SecurityManager.SecurityEnabled) {
82 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
88 // Note: we do not ask for PathDiscovery because no path is returned here.
89 // However MS Fx requires it (see FDBK23572 for details).
94 public string ScopeName {
95 get { return scopename; }
99 public ModuleHandle ModuleHandle {
101 return new ModuleHandle (_impl);
105 public extern int MetadataToken {
106 [MethodImplAttribute (MethodImplOptions.InternalCall)]
110 public extern int MDStreamVersion {
111 [MethodImplAttribute (MethodImplOptions.InternalCall)]
116 public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
118 System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
119 Type[] types = GetTypes ();
120 foreach (Type t in types)
121 if (filter (t, filterCriteria))
123 return (Type[])filtered.ToArray (typeof(Type));
126 public virtual object[] GetCustomAttributes(bool inherit)
128 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
131 public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
133 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
136 public FieldInfo GetField (string name)
141 Type globalType = GetGlobalType ();
142 return (globalType != null) ? globalType.GetField (name, BindingFlags.Public | BindingFlags.Static) : null;
145 public FieldInfo GetField (string name, BindingFlags flags)
150 Type globalType = GetGlobalType ();
151 return (globalType != null) ? globalType.GetField (name, flags) : null;
154 public FieldInfo[] GetFields ()
157 return new FieldInfo [0];
159 Type globalType = GetGlobalType ();
160 return (globalType != null) ? globalType.GetFields (BindingFlags.Public | BindingFlags.Static) : new FieldInfo [0];
163 public MethodInfo GetMethod (string name)
165 // Can't call the other overloads since they call Type.GetMethod () which does a null check on the 'types' array
169 Type globalType = GetGlobalType ();
170 return (globalType != null) ? globalType.GetMethod (name) : null;
173 public MethodInfo GetMethod (string name, Type[] types)
175 return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
178 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
180 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
183 protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
188 Type globalType = GetGlobalType ();
189 return (globalType != null) ? globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers) : null;
192 public MethodInfo[] GetMethods ()
195 return new MethodInfo [0];
197 Type globalType = GetGlobalType ();
198 return (globalType != null) ? globalType.GetMethods () : new MethodInfo [0];
202 public MethodInfo[] GetMethods (BindingFlags flags) {
204 return new MethodInfo [0];
206 Type globalType = GetGlobalType ();
207 return (globalType != null) ? globalType.GetMethods (flags) : new MethodInfo [0];
210 public FieldInfo[] GetFields (BindingFlags flags)
213 return new FieldInfo [0];
215 Type globalType = GetGlobalType ();
216 return (globalType != null) ? globalType.GetFields (flags) : new FieldInfo [0];
220 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
221 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
224 throw new ArgumentNullException ("info");
226 UnitySerializationHolder.GetModuleData (this, info, context);
229 public X509Certificate GetSignerCertificate ()
232 return X509Certificate.CreateFromSignedFile (assembly.Location);
242 public virtual Type GetType(string className)
244 return GetType (className, false, false);
250 public virtual Type GetType(string className, bool ignoreCase)
252 return GetType (className, false, ignoreCase);
258 public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
260 if (className == null)
261 throw new ArgumentNullException ("className");
262 if (className == String.Empty)
263 throw new ArgumentException ("Type name can't be empty");
264 return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
267 [MethodImplAttribute (MethodImplOptions.InternalCall)]
268 private extern Type[] InternalGetTypes ();
270 public virtual Type[] GetTypes()
272 return InternalGetTypes ();
275 public virtual bool IsDefined (Type attributeType, bool inherit)
277 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
280 public bool IsResource()
285 public override string ToString ()
292 return Mono_GetGuid (this);
297 public Guid ModuleVersionId {
299 return Mono_GetGuid (this);
303 public void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
304 ModuleHandle.GetPEKind (out peKind, out machine);
310 private Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
311 if (error == ResolveTokenError.OutOfRange)
312 return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
314 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");
317 public FieldInfo ResolveField (int metadataToken) {
318 ResolveTokenError error;
320 IntPtr handle = ResolveFieldToken (_impl, metadataToken, out error);
321 if (handle == IntPtr.Zero)
322 throw resolve_token_exception (metadataToken, error, "Field");
324 return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
327 public MemberInfo ResolveMember (int metadataToken) {
328 ResolveTokenError error;
330 MemberInfo m = ResolveMemberToken (_impl, metadataToken, out error);
332 throw resolve_token_exception (metadataToken, error, "MemberInfo");
337 public MethodBase ResolveMethod (int metadataToken) {
338 ResolveTokenError error;
340 IntPtr handle = ResolveMethodToken (_impl, metadataToken, out error);
341 if (handle == IntPtr.Zero)
342 throw resolve_token_exception (metadataToken, error, "MethodBase");
344 return MethodBase.GetMethodFromHandle (new RuntimeMethodHandle (handle));
347 public string ResolveString (int metadataToken) {
348 ResolveTokenError error;
350 string s = ResolveStringToken (_impl, metadataToken, out error);
352 throw resolve_token_exception (metadataToken, error, "string");
357 public Type ResolveType (int metadataToken) {
358 ResolveTokenError error;
360 IntPtr handle = ResolveTypeToken (_impl, metadataToken, out error);
361 if (handle == IntPtr.Zero)
362 throw resolve_token_exception (metadataToken, error, "Type");
364 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
368 public byte[] ResolveSignature (int metadataToken) {
369 throw new NotImplementedException ();
373 internal static Type MonoDebugger_ResolveType (Module module, int token)
375 ResolveTokenError error;
377 IntPtr handle = ResolveTypeToken (module._impl, token, out error);
378 if (handle == IntPtr.Zero)
381 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
384 // Mono Extension: returns the GUID of this module
385 internal static Guid Mono_GetGuid (Module module)
387 return new Guid (module.GetGuidInternal ());
390 private static bool filter_by_type_name (Type m, object filterCriteria) {
391 string s = (string)filterCriteria;
392 if (s.EndsWith ("*"))
393 return m.Name.StartsWith (s.Substring (0, s.Length - 1));
398 private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
399 string s = (string)filterCriteria;
400 if (s.EndsWith ("*"))
401 return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
403 return String.Compare (m.Name, s, true) == 0;
406 [MethodImplAttribute (MethodImplOptions.InternalCall)]
407 private extern string GetGuidInternal ();
409 [MethodImplAttribute (MethodImplOptions.InternalCall)]
410 private extern Type GetGlobalType ();
412 [MethodImplAttribute (MethodImplOptions.InternalCall)]
413 internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, out ResolveTokenError error);
415 [MethodImplAttribute (MethodImplOptions.InternalCall)]
416 internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, out ResolveTokenError error);
418 [MethodImplAttribute (MethodImplOptions.InternalCall)]
419 internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, out ResolveTokenError error);
421 [MethodImplAttribute (MethodImplOptions.InternalCall)]
422 internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
424 [MethodImplAttribute (MethodImplOptions.InternalCall)]
425 internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, out ResolveTokenError error);
427 [MethodImplAttribute (MethodImplOptions.InternalCall)]
428 internal static extern void GetPEKind (IntPtr module, out PortableExecutableKinds peKind, out ImageFileMachine machine);
431 void _Module.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
433 throw new NotImplementedException ();
436 void _Module.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
438 throw new NotImplementedException ();
441 void _Module.GetTypeInfoCount (out uint pcTInfo)
443 throw new NotImplementedException ();
446 void _Module.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
447 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
449 throw new NotImplementedException ();