0830cec58f2d922627f0051cd2601cf3e60242d4
[mono.git] / mcs / class / corlib / System.Reflection / Module.cs
1 //
2 // System.Reflection/Module.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
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;
37
38 namespace System.Reflection {
39
40         internal enum ResolveTokenError {
41                 OutOfRange,
42                 BadTable,
43                 Other
44         };
45
46         [ComVisible (true)]
47         [ComDefaultInterfaceAttribute (typeof (_Module))]
48         [Serializable]
49         [ClassInterfaceAttribute (ClassInterfaceType.None)]
50
51 #if NET_4_0
52         public class Module : ISerializable, ICustomAttributeProvider, _Module {
53 #else
54         public partial class Module : ISerializable, ICustomAttributeProvider, _Module {
55 #endif
56                 public static readonly TypeFilter FilterTypeName;
57                 public static readonly TypeFilter FilterTypeNameIgnoreCase;
58         
59 #pragma warning disable 649     
60                 internal IntPtr _impl; /* a pointer to a MonoImage */
61                 internal Assembly assembly;
62                 internal string fqname;
63                 internal string name;
64                 internal string scopename;
65                 internal bool is_resource;
66                 internal int token;
67 #pragma warning restore 649             
68         
69                 const BindingFlags defaultBindingFlags = 
70                         BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
71                 
72                 static Module () {
73                         FilterTypeName = new TypeFilter (filter_by_type_name);
74                         FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
75                 }
76
77
78 #if NET_4_0
79                 protected
80 #else
81                 internal
82 #endif
83                 Module () {
84                 }
85
86                 public Assembly Assembly {
87                         get { return assembly; }
88                 }
89         
90                 public virtual string FullyQualifiedName {
91                         get {
92 #if !NET_2_1
93                                 if (SecurityManager.SecurityEnabled) {
94                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
95                                 }
96 #endif
97                                 return fqname;
98                         }
99                 }
100
101                 // Note: we do not ask for PathDiscovery because no path is returned here.
102                 // However MS Fx requires it (see FDBK23572 for details).
103                 public string Name {
104                         get { return name; }
105                 }
106         
107                 public string ScopeName {
108                         get { return scopename; }
109                 }
110
111                 public ModuleHandle ModuleHandle {
112                         get {
113                                 return new ModuleHandle (_impl);
114                         }
115                 }
116
117                 public extern int MetadataToken {
118                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
119                         get;
120                 }
121
122                 public int MDStreamVersion {
123                         get {
124                                 if (_impl == IntPtr.Zero)
125                                         throw new NotSupportedException ();
126                                 return GetMDStreamVersion (_impl);
127                         }
128                 }
129
130                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
131                 internal static extern int GetMDStreamVersion (IntPtr module_handle);
132
133                 public FieldInfo GetField (string name) 
134                 {
135                         return GetField (name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
136                 }
137
138                 public FieldInfo[] GetFields () 
139                 {
140                         return GetFields (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
141                 }
142         
143                 public MethodInfo GetMethod (string name) 
144                 {
145                         //FIXME this sure breaks since Type.GetMethod throws due to a null 'type' array. But it's what MS does
146                         return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, null, null);
147                 }
148         
149                 public MethodInfo GetMethod (string name, Type[] types) 
150                 {
151                         return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
152                 }
153         
154                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
155                 {
156                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
157                 }
158         
159                 public MethodInfo[] GetMethods () 
160                 {
161                         return GetMethods (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
162                 }
163         
164                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
165                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context) 
166                 {
167                         if (info == null)
168                                 throw new ArgumentNullException ("info");
169
170                         UnitySerializationHolder.GetModuleData (this, info, context);
171                 }
172
173                 [ComVisible (true)]
174                 public virtual Type GetType(string className) 
175                 {
176                         return GetType (className, false, false);
177                 }
178
179                 [ComVisible (true)]
180                 public virtual Type GetType(string className, bool ignoreCase) 
181                 {
182                         return GetType (className, false, ignoreCase);
183                 }
184         
185                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186                 internal extern Type[] InternalGetTypes ();
187         
188                 public override string ToString () 
189                 {
190                         return name;
191                 }
192
193                 internal Guid MvId {
194                         get {
195                                 return GetModuleVersionId ();
196                         }
197                 }
198
199                 public Guid ModuleVersionId {
200                         get {
201                                 return GetModuleVersionId ();
202                         }
203                 }
204
205                 internal Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
206                         if (error == ResolveTokenError.OutOfRange)
207                                 return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
208                         else
209                                 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");
210                 }
211
212                 internal IntPtr[] ptrs_from_types (Type[] types) {
213                         if (types == null)
214                                 return null;
215                         else {
216                                 IntPtr[] res = new IntPtr [types.Length];
217                                 for (int i = 0; i < types.Length; ++i) {
218                                         if (types [i] == null)
219                                                 throw new ArgumentException ();
220                                         res [i] = types [i].TypeHandle.Value;
221                                 }
222                                 return res;
223                         }
224                 }
225
226                 public FieldInfo ResolveField (int metadataToken) {
227                         return ResolveField (metadataToken, null, null);
228                 }
229
230                 public MemberInfo ResolveMember (int metadataToken) {
231                         return ResolveMember (metadataToken, null, null);
232                 }
233
234                 public MethodBase ResolveMethod (int metadataToken) {
235                         return ResolveMethod (metadataToken, null, null);
236                 }
237
238                 public Type ResolveType (int metadataToken) {
239                         return ResolveType (metadataToken, null, null);
240                 }
241
242                 internal static Type MonoDebugger_ResolveType (Module module, int token)
243                 {
244                         ResolveTokenError error;
245
246                         IntPtr handle = ResolveTypeToken (module._impl, token, null, null, out error);
247                         if (handle == IntPtr.Zero)
248                                 return null;
249                         else
250                                 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
251                 }
252
253                 // Used by mcs, the symbol writer, and mdb through reflection
254                 internal static Guid Mono_GetGuid (Module module)
255                 {
256                         return module.GetModuleVersionId ();
257                 }
258
259                 internal virtual Guid GetModuleVersionId ()
260                 {
261                         return new Guid (GetGuidInternal ());
262                 }
263
264                 private static bool filter_by_type_name (Type m, object filterCriteria) {
265                         string s = (string)filterCriteria;
266                         if (s.EndsWith ("*"))
267                                 return m.Name.StartsWith (s.Substring (0, s.Length - 1));
268                         else
269                                 return m.Name == s;
270                 }
271
272                 private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
273                         string s = (string)filterCriteria;
274                         if (s.EndsWith ("*"))
275                                 return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
276                         else
277                                 return String.Compare (m.Name, s, true) == 0;
278                 }
279
280                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
281                 internal extern IntPtr GetHINSTANCE ();
282
283                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
284                 private extern string GetGuidInternal ();
285
286                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
287                 internal extern Type GetGlobalType ();
288
289                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
290                 internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
291
292                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
293                 internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
294
295                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
296                 internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
297
298                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
299                 internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
300
301                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
302                 internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
303
304                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
305                 internal static extern byte[] ResolveSignature (IntPtr module, int metadataToken, out ResolveTokenError error);
306
307                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
308                 internal static extern void GetPEKind (IntPtr module, out PortableExecutableKinds peKind, out ImageFileMachine machine);
309
310                 void _Module.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
311                 {
312                         throw new NotImplementedException ();
313                 }
314
315                 void _Module.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
316                 {
317                         throw new NotImplementedException ();
318                 }
319
320                 void _Module.GetTypeInfoCount (out uint pcTInfo)
321                 {
322                         throw new NotImplementedException ();
323                 }
324
325                 void _Module.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
326                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
327                 {
328                         throw new NotImplementedException ();
329                 }
330
331 #if NET_4_0
332                 static Exception CreateNIE ()
333                 {
334                         return new NotImplementedException ("Derived classes must implement it");
335                 }
336
337                 public virtual bool IsResource()
338                 {
339                         throw CreateNIE ();
340                 }
341
342                 public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria) 
343                 {
344                         throw CreateNIE ();
345                 }
346
347                 public virtual object[] GetCustomAttributes(bool inherit)
348                 {
349                         throw CreateNIE ();
350                 }
351
352                 public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) 
353                 {
354                         throw CreateNIE ();
355                 }
356
357                 public virtual IList<CustomAttributeData> GetCustomAttributesData ()
358                 {
359                         throw CreateNIE ();
360                 }
361
362                 public virtual FieldInfo GetField (string name, BindingFlags bindingAttr) 
363                 {
364                         throw CreateNIE ();
365                 }
366
367                 public virtual FieldInfo[] GetFields (BindingFlags bindingFlags)
368                 {
369                         throw CreateNIE ();
370                 }
371
372                 protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
373                 {
374                         throw CreateNIE ();
375                 }
376
377                 public virtual MethodInfo[] GetMethods (BindingFlags bindingFlags)
378                 {
379                         throw CreateNIE ();
380                 }
381
382                 public virtual void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine)
383                 {
384                         throw CreateNIE ();
385                 }
386
387                 [ComVisible (true)]
388                 public virtual Type GetType(string className, bool throwOnError, bool ignoreCase) 
389                 {
390                         throw CreateNIE ();
391                 }
392
393                 public virtual bool IsDefined (Type attributeType, bool inherit) 
394                 {
395                         throw CreateNIE ();
396                 }
397
398                 public virtual FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
399                 {
400                         throw CreateNIE ();
401                 }
402
403                 public virtual MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
404                 {
405                         throw CreateNIE ();
406                 }
407
408                 public virtual MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
409                 {
410                         throw CreateNIE ();
411                 }
412
413                 public virtual byte[] ResolveSignature (int metadataToken)
414                 {
415                         throw CreateNIE ();
416                 }
417
418                 public virtual string ResolveString (int metadataToken)
419                 {
420                         throw CreateNIE ();
421                 }
422
423                 public virtual Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
424                 {
425                         throw CreateNIE ();
426                 }
427
428                 public virtual X509Certificate GetSignerCertificate ()
429                 {
430                         throw CreateNIE ();
431                 }
432
433                 public virtual Type[] GetTypes() 
434                 {
435                         throw CreateNIE ();
436                 }
437 #endif
438
439         }
440 }