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