Merge pull request #409 from Alkarex/patch-1
[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         [StructLayout (LayoutKind.Sequential)]
51 #if NET_4_0 || MOONLIGHT || MOBILE
52         public abstract class Module : ISerializable, ICustomAttributeProvider, _Module {
53 #else
54         public partial class Module : ISerializable, ICustomAttributeProvider, _Module {
55 #endif
56                 public static readonly TypeFilter FilterTypeName = new TypeFilter (filter_by_type_name);
57                 public static readonly TypeFilter FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
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 #if NET_4_0 || MOONLIGHT || MOBILE
73                 protected
74 #else
75                 internal
76 #endif
77                 Module () {
78                 }
79
80                 public ModuleHandle ModuleHandle {
81                         get {
82                                 return new ModuleHandle (_impl);
83                         }
84                 }
85
86                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
87                 internal static extern int get_MetadataToken (Module module);
88                 
89                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
90                 internal static extern int GetMDStreamVersion (IntPtr module_handle);
91
92                 public FieldInfo GetField (string name) 
93                 {
94                         return GetField (name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
95                 }
96
97                 public FieldInfo[] GetFields () 
98                 {
99                         return GetFields (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
100                 }
101         
102                 public MethodInfo GetMethod (string name) 
103                 {
104                         return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, null, null);
105                 }
106         
107                 public MethodInfo GetMethod (string name, Type[] types) 
108                 {
109                         if (types == null)
110                                 throw new ArgumentNullException ("types");
111                         return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
112                 }
113         
114                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
115                 {
116                         if (types == null)
117                                 throw new ArgumentNullException ("types");
118                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
119                 }
120         
121                 public MethodInfo[] GetMethods () 
122                 {
123                         return GetMethods (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
124                 }
125         
126                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
127                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context) 
128                 {
129                         if (info == null)
130                                 throw new ArgumentNullException ("info");
131
132                         UnitySerializationHolder.GetModuleData (this, info, context);
133                 }
134
135                 [ComVisible (true)]
136                 public virtual Type GetType(string className) 
137                 {
138                         return GetType (className, false, false);
139                 }
140
141                 [ComVisible (true)]
142                 public virtual Type GetType(string className, bool ignoreCase) 
143                 {
144                         return GetType (className, false, ignoreCase);
145                 }
146         
147                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
148                 internal extern Type[] InternalGetTypes ();
149         
150                 public override string ToString () 
151                 {
152                         return name;
153                 }
154
155                 internal Guid MvId {
156                         get {
157                                 return GetModuleVersionId ();
158                         }
159                 }
160
161                 internal Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
162                         if (error == ResolveTokenError.OutOfRange)
163                                 return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
164                         else
165                                 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");
166                 }
167
168                 internal IntPtr[] ptrs_from_types (Type[] types) {
169                         if (types == null)
170                                 return null;
171                         else {
172                                 IntPtr[] res = new IntPtr [types.Length];
173                                 for (int i = 0; i < types.Length; ++i) {
174                                         if (types [i] == null)
175                                                 throw new ArgumentException ();
176                                         res [i] = types [i].TypeHandle.Value;
177                                 }
178                                 return res;
179                         }
180                 }
181
182                 public FieldInfo ResolveField (int metadataToken) {
183                         return ResolveField (metadataToken, null, null);
184                 }
185
186                 public MemberInfo ResolveMember (int metadataToken) {
187                         return ResolveMember (metadataToken, null, null);
188                 }
189
190                 public MethodBase ResolveMethod (int metadataToken) {
191                         return ResolveMethod (metadataToken, null, null);
192                 }
193
194                 public Type ResolveType (int metadataToken) {
195                         return ResolveType (metadataToken, null, null);
196                 }
197
198                 internal static Type MonoDebugger_ResolveType (Module module, int token)
199                 {
200                         ResolveTokenError error;
201
202                         IntPtr handle = ResolveTypeToken (module._impl, token, null, null, out error);
203                         if (handle == IntPtr.Zero)
204                                 return null;
205                         else
206                                 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
207                 }
208
209                 // Used by mcs, the symbol writer, and mdb through reflection
210                 internal static Guid Mono_GetGuid (Module module)
211                 {
212                         return module.GetModuleVersionId ();
213                 }
214
215                 internal virtual Guid GetModuleVersionId ()
216                 {
217                         return new Guid (GetGuidInternal ());
218                 }
219
220                 private static bool filter_by_type_name (Type m, object filterCriteria) {
221                         string s = (string)filterCriteria;
222                         if (s.EndsWith ("*"))
223                                 return m.Name.StartsWith (s.Substring (0, s.Length - 1));
224                         else
225                                 return m.Name == s;
226                 }
227
228                 private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
229                         string s = (string)filterCriteria;
230                         if (s.EndsWith ("*"))
231                                 return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
232                         else
233                                 return String.Compare (m.Name, s, true) == 0;
234                 }
235
236                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
237                 internal extern IntPtr GetHINSTANCE ();
238
239                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
240                 private extern string GetGuidInternal ();
241
242                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
243                 internal extern Type GetGlobalType ();
244
245                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246                 internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
247
248                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
249                 internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
250
251                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
252                 internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
253
254                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
255                 internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
256
257                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
258                 internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, IntPtr[] type_args, IntPtr[] method_args, out ResolveTokenError error);
259
260                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
261                 internal static extern byte[] ResolveSignature (IntPtr module, int metadataToken, out ResolveTokenError error);
262
263                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
264                 internal static extern void GetPEKind (IntPtr module, out PortableExecutableKinds peKind, out ImageFileMachine machine);
265
266                 void _Module.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
267                 {
268                         throw new NotImplementedException ();
269                 }
270
271                 void _Module.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
272                 {
273                         throw new NotImplementedException ();
274                 }
275
276                 void _Module.GetTypeInfoCount (out uint pcTInfo)
277                 {
278                         throw new NotImplementedException ();
279                 }
280
281                 void _Module.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
282                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
283                 {
284                         throw new NotImplementedException ();
285                 }
286
287 #if NET_4_0
288                 public override bool Equals (object o)
289                 {
290                         return o == (object) this;
291                 }
292
293                 public override int GetHashCode ()
294                 {
295                         return base.GetHashCode ();
296                 }
297
298                 public static bool operator == (Module left, Module right)
299                 {
300                         if ((object)left == (object)right)
301                                 return true;
302                         if ((object)left == null ^ (object)right == null)
303                                 return false;
304                         return left.Equals (right);
305                 }
306
307                 public static bool operator != (Module left, Module right)
308                 {
309                         if ((object)left == (object)right)
310                                 return false;
311                         if ((object)left == null ^ (object)right == null)
312                                 return true;
313                         return !left.Equals (right);
314                 }
315
316 #endif
317
318 #if NET_4_0 || MOONLIGHT || MOBILE
319
320                 public virtual Assembly Assembly {
321                         get { throw CreateNIE (); }
322                 }
323
324                 public virtual string Name {
325                         get { throw CreateNIE (); }
326                 }
327         
328                 public virtual string ScopeName {
329                         get { throw CreateNIE (); }
330                 }
331
332                 public virtual int MDStreamVersion {
333                         get { throw CreateNIE (); }
334                 }
335
336                 public virtual Guid ModuleVersionId {
337                         get { throw CreateNIE (); }
338                 }
339
340                 public virtual string FullyQualifiedName {
341                         get { throw CreateNIE (); }
342                 }
343
344                 public virtual int MetadataToken {
345                         get { throw CreateNIE (); }
346                 }
347
348                 static Exception CreateNIE ()
349                 {
350                         return new NotImplementedException ("Derived classes must implement it");
351                 }
352
353                 public virtual bool IsResource()
354                 {
355                         throw CreateNIE ();
356                 }
357
358                 public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria) 
359                 {
360                         throw CreateNIE ();
361                 }
362
363                 public virtual object[] GetCustomAttributes(bool inherit)
364                 {
365                         throw CreateNIE ();
366                 }
367
368                 public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) 
369                 {
370                         throw CreateNIE ();
371                 }
372
373                 public virtual IList<CustomAttributeData> GetCustomAttributesData ()
374                 {
375                         throw CreateNIE ();
376                 }
377
378                 public virtual FieldInfo GetField (string name, BindingFlags bindingAttr) 
379                 {
380                         throw CreateNIE ();
381                 }
382
383                 public virtual FieldInfo[] GetFields (BindingFlags bindingFlags)
384                 {
385                         throw CreateNIE ();
386                 }
387
388                 protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
389                 {
390                         throw CreateNIE ();
391                 }
392
393                 public virtual MethodInfo[] GetMethods (BindingFlags bindingFlags)
394                 {
395                         throw CreateNIE ();
396                 }
397
398                 public virtual void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine)
399                 {
400                         throw CreateNIE ();
401                 }
402
403                 [ComVisible (true)]
404                 public virtual Type GetType(string className, bool throwOnError, bool ignoreCase) 
405                 {
406                         throw CreateNIE ();
407                 }
408
409                 public virtual bool IsDefined (Type attributeType, bool inherit) 
410                 {
411                         throw CreateNIE ();
412                 }
413
414                 public virtual FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
415                 {
416                         throw CreateNIE ();
417                 }
418
419                 public virtual MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
420                 {
421                         throw CreateNIE ();
422                 }
423
424                 public virtual MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
425                 {
426                         throw CreateNIE ();
427                 }
428
429                 public virtual byte[] ResolveSignature (int metadataToken)
430                 {
431                         throw CreateNIE ();
432                 }
433
434                 public virtual string ResolveString (int metadataToken)
435                 {
436                         throw CreateNIE ();
437                 }
438
439                 public virtual Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments)
440                 {
441                         throw CreateNIE ();
442                 }
443
444                 public virtual X509Certificate GetSignerCertificate ()
445                 {
446                         throw CreateNIE ();
447                 }
448
449                 public virtual Type[] GetTypes() 
450                 {
451                         throw CreateNIE ();
452                 }
453 #endif
454
455         }
456 }