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;
36 using System.Collections.Generic;
38 namespace System.Reflection {
40 internal enum ResolveTokenError {
47 [ComDefaultInterfaceAttribute (typeof (_Module))]
49 [ClassInterfaceAttribute (ClassInterfaceType.None)]
52 public abstract class Module : ISerializable, ICustomAttributeProvider, _Module {
54 public partial class Module : ISerializable, ICustomAttributeProvider, _Module {
56 public static readonly TypeFilter FilterTypeName;
57 public static readonly TypeFilter FilterTypeNameIgnoreCase;
59 #pragma warning disable 649
60 internal IntPtr _impl; /* a pointer to a MonoImage */
61 internal Assembly assembly;
62 internal string fqname;
64 internal string scopename;
65 internal bool is_resource;
67 #pragma warning restore 649
69 const BindingFlags defaultBindingFlags =
70 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
73 FilterTypeName = new TypeFilter (filter_by_type_name);
74 FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
86 public ModuleHandle ModuleHandle {
88 return new ModuleHandle (_impl);
92 [MethodImplAttribute (MethodImplOptions.InternalCall)]
93 internal static extern int get_MetadataToken (Module module);
95 [MethodImplAttribute (MethodImplOptions.InternalCall)]
96 internal static extern int GetMDStreamVersion (IntPtr module_handle);
98 public FieldInfo GetField (string name)
100 return GetField (name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
103 public FieldInfo[] GetFields ()
105 return GetFields (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
108 public MethodInfo GetMethod (string name)
110 return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, null, null);
113 public MethodInfo GetMethod (string name, Type[] types)
116 throw new ArgumentNullException ("types");
117 return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
120 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
123 throw new ArgumentNullException ("types");
124 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
127 public MethodInfo[] GetMethods ()
129 return GetMethods (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
132 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
133 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
136 throw new ArgumentNullException ("info");
138 UnitySerializationHolder.GetModuleData (this, info, context);
142 public virtual Type GetType(string className)
144 return GetType (className, false, false);
148 public virtual Type GetType(string className, bool ignoreCase)
150 return GetType (className, false, ignoreCase);
153 [MethodImplAttribute (MethodImplOptions.InternalCall)]
154 internal extern Type[] InternalGetTypes ();
156 public override string ToString ()
163 return GetModuleVersionId ();
167 internal Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
168 if (error == ResolveTokenError.OutOfRange)
169 return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
171 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");
174 internal IntPtr[] ptrs_from_types (Type[] types) {
178 IntPtr[] res = new IntPtr [types.Length];
179 for (int i = 0; i < types.Length; ++i) {
180 if (types [i] == null)
181 throw new ArgumentException ();
182 res [i] = types [i].TypeHandle.Value;
188 public FieldInfo ResolveField (int metadataToken) {
189 return ResolveField (metadataToken, null, null);
192 public MemberInfo ResolveMember (int metadataToken) {
193 return ResolveMember (metadataToken, null, null);
196 public MethodBase ResolveMethod (int metadataToken) {
197 return ResolveMethod (metadataToken, null, null);
200 public Type ResolveType (int metadataToken) {
201 return ResolveType (metadataToken, null, null);
204 internal static Type MonoDebugger_ResolveType (Module module, int token)
206 ResolveTokenError error;
208 IntPtr handle = ResolveTypeToken (module._impl, token, null, null, out error);
209 if (handle == IntPtr.Zero)
212 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
215 // Used by mcs, the symbol writer, and mdb through reflection
216 internal static Guid Mono_GetGuid (Module module)
218 return module.GetModuleVersionId ();
221 internal virtual Guid GetModuleVersionId ()
223 return new Guid (GetGuidInternal ());
226 private static bool filter_by_type_name (Type m, object filterCriteria) {
227 string s = (string)filterCriteria;
228 if (s.EndsWith ("*"))
229 return m.Name.StartsWith (s.Substring (0, s.Length - 1));
234 private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
235 string s = (string)filterCriteria;
236 if (s.EndsWith ("*"))
237 return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
239 return String.Compare (m.Name, s, true) == 0;
242 [MethodImplAttribute (MethodImplOptions.InternalCall)]
243 internal extern IntPtr GetHINSTANCE ();
245 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246 private extern string GetGuidInternal ();
248 [MethodImplAttribute (MethodImplOptions.InternalCall)]
249 internal extern Type GetGlobalType ();
251 [MethodImplAttribute (MethodImplOptions.InternalCall)]
252 internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
254 [MethodImplAttribute (MethodImplOptions.InternalCall)]
255 internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
257 [MethodImplAttribute (MethodImplOptions.InternalCall)]
258 internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
260 [MethodImplAttribute (MethodImplOptions.InternalCall)]
261 internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
263 [MethodImplAttribute (MethodImplOptions.InternalCall)]
264 internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
266 [MethodImplAttribute (MethodImplOptions.InternalCall)]
267 internal static extern byte[] ResolveSignature (IntPtr module, int metadataToken, out ResolveTokenError error);
269 [MethodImplAttribute (MethodImplOptions.InternalCall)]
270 internal static extern void GetPEKind (IntPtr module, out PortableExecutableKinds peKind, out ImageFileMachine machine);
272 void _Module.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
274 throw new NotImplementedException ();
277 void _Module.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
279 throw new NotImplementedException ();
282 void _Module.GetTypeInfoCount (out uint pcTInfo)
284 throw new NotImplementedException ();
287 void _Module.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
288 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
290 throw new NotImplementedException ();
295 public virtual Assembly Assembly {
296 get { throw CreateNIE (); }
299 public virtual string Name {
300 get { throw CreateNIE (); }
303 public virtual string ScopeName {
304 get { throw CreateNIE (); }
307 public virtual int MDStreamVersion {
308 get { throw CreateNIE (); }
311 public virtual Guid ModuleVersionId {
312 get { throw CreateNIE (); }
315 public virtual string FullyQualifiedName {
316 get { throw CreateNIE (); }
319 public virtual int MetadataToken {
320 get { throw CreateNIE (); }
323 static Exception CreateNIE ()
325 return new NotImplementedException ("Derived classes must implement it");
328 public virtual bool IsResource()
333 public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria)
338 public virtual object[] GetCustomAttributes(bool inherit)
343 public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
348 public virtual IList<CustomAttributeData> GetCustomAttributesData ()
353 public virtual FieldInfo GetField (string name, BindingFlags bindingAttr)
358 public virtual FieldInfo[] GetFields (BindingFlags bindingFlags)
363 protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
368 public virtual MethodInfo[] GetMethods (BindingFlags bindingFlags)
373 public virtual void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine)
379 public virtual Type GetType(string className, bool throwOnError, bool ignoreCase)
384 public virtual bool IsDefined (Type attributeType, bool inherit)
389 public virtual FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
394 public virtual MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
399 public virtual MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
404 public virtual byte[] ResolveSignature (int metadataToken)
409 public virtual string ResolveString (int metadataToken)
414 public virtual Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
419 public virtual X509Certificate GetSignerCertificate ()
424 public virtual Type[] GetTypes()