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