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