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