2005-05-06 Zoltan Varga <vargaz@freemail.hu>
[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
37 namespace System.Reflection {
38
39         internal enum ResolveTokenError {
40                 OutOfRange,
41                 BadTable,
42                 Other
43         };
44
45         [Serializable]
46         public class Module : ISerializable, ICustomAttributeProvider {
47         
48                 public static readonly TypeFilter FilterTypeName;
49                 public static readonly TypeFilter FilterTypeNameIgnoreCase;
50         
51                 private IntPtr _impl; /* a pointer to a MonoImage */
52                 internal Assembly assembly;
53                 internal string fqname;
54                 internal string name;
55                 internal string scopename;
56                 internal bool is_resource;
57                 internal int token;
58         
59                 const BindingFlags defaultBindingFlags = 
60                         BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
61                 
62                 static Module () {
63                         FilterTypeName = new TypeFilter (filter_by_type_name);
64                         FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
65                 }
66
67                 internal Module () {
68                 }
69
70                 public Assembly Assembly {
71                         get { return assembly; }
72                 }
73         
74                 public virtual string FullyQualifiedName {
75                         get {
76                                 if (SecurityManager.SecurityEnabled) {
77                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
78                                 }
79                                 return fqname;
80                         }
81                 }
82
83                 // Note: we do not ask for PathDiscovery because no path is returned here.
84                 // However MS Fx requires it (see FDBK23572 for details).
85                 public string Name {
86                         get { return name; }
87                 }
88         
89                 public string ScopeName {
90                         get { return scopename; }
91                 }
92
93 #if NET_2_0
94                 [CLSCompliant(false)]
95                 public ModuleHandle ModuleHandle {
96                         get {
97                                 return new ModuleHandle (_impl);
98                         }
99                 }
100
101                 public extern int MetadataToken {
102                         [MethodImplAttribute (MethodImplOptions.InternalCall)]
103                         get;
104                 }               
105 #endif
106         
107                 public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria) 
108                 {
109                         System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
110                         Type[] types = GetTypes ();
111                         foreach (Type t in types)
112                                 if (filter (t, filterCriteria))
113                                         filtered.Add (t);
114                         return (Type[])filtered.ToArray (typeof(Type));
115                 }
116         
117                 public virtual object[] GetCustomAttributes(bool inherit) 
118                 {
119                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
120                 }
121         
122                 public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) 
123                 {
124                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
125                 }
126         
127                 public FieldInfo GetField (string name) 
128                 {
129                         if (IsResource ())
130                                 return null;
131
132                         Type globalType = GetGlobalType ();
133                         return (globalType != null) ? globalType.GetField (name, BindingFlags.Public | BindingFlags.Static) : null;
134                 }
135         
136                 public FieldInfo GetField (string name, BindingFlags flags) 
137                 {
138                         if (IsResource ())
139                                 return null;
140
141                         Type globalType = GetGlobalType ();
142                         return (globalType != null) ? globalType.GetField (name, flags) : null;
143                 }
144         
145                 public FieldInfo[] GetFields () 
146                 {
147                         if (IsResource ())
148                                 return new FieldInfo [0];
149
150                         Type globalType = GetGlobalType ();
151                         return (globalType != null) ? globalType.GetFields (BindingFlags.Public | BindingFlags.Static) : new FieldInfo [0];
152                 }
153         
154                 public MethodInfo GetMethod (string name) 
155                 {
156                         return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, Type.EmptyTypes, null);
157                 }
158         
159                 public MethodInfo GetMethod (string name, Type[] types) 
160                 {
161                         return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
162                 }
163         
164                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
165                 {
166                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
167                 }
168         
169                 protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
170                 {
171                         if (IsResource ())
172                                 return null;
173
174                         Type globalType = GetGlobalType ();
175                         return (globalType != null) ? globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers) : null;
176                 }
177         
178                 public MethodInfo[] GetMethods () 
179                 {
180                         if (IsResource ())
181                                 return new MethodInfo [0];
182
183                         Type globalType = GetGlobalType ();
184                         return (globalType != null) ? globalType.GetMethods () : new MethodInfo [0];
185                 }
186
187 #if NET_2_0
188                 public MethodInfo[] GetMethods (BindingFlags flags) {
189                         if (IsResource ())
190                                 return new MethodInfo [0];
191
192                         Type globalType = GetGlobalType ();
193                         return (globalType != null) ? globalType.GetMethods (flags) : new MethodInfo [0];
194                 }
195
196                 public FieldInfo[] GetFields (BindingFlags flags) 
197                 {
198                         if (IsResource ())
199                                 return new FieldInfo [0];
200
201                         Type globalType = GetGlobalType ();
202                         return (globalType != null) ? globalType.GetFields (flags) : new FieldInfo [0];
203                 }
204 #endif
205         
206                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
207                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context) 
208                 {
209                         if (info == null)
210                                 throw new ArgumentNullException ("info");
211
212                         UnitySerializationHolder.GetModuleData (this, info, context);
213                 }
214         
215                 public X509Certificate GetSignerCertificate ()
216                 {
217                         try {
218                                 return X509Certificate.CreateFromSignedFile (assembly.Location);
219                         }
220                         catch {
221                                 return null;
222                         }
223                 }
224         
225                 public virtual Type GetType(string className) 
226                 {
227                         return GetType (className, false, false);
228                 }
229         
230                 public virtual Type GetType(string className, bool ignoreCase) 
231                 {
232                         return GetType (className, false, ignoreCase);
233                 }
234         
235                 public virtual Type GetType(string className, bool throwOnError, bool ignoreCase) 
236                 {
237                         if (className == null)
238                                 throw new ArgumentNullException ("className");
239                         if (className == String.Empty)
240                                 throw new ArgumentException ("Type name can't be empty");
241                         return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
242                 }
243
244                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
245                 private extern Type[] InternalGetTypes ();
246         
247                 public virtual Type[] GetTypes() 
248                 {
249                         return InternalGetTypes ();
250                 }
251         
252                 public virtual bool IsDefined (Type attributeType, bool inherit) 
253                 {
254                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
255                 }
256         
257                 public bool IsResource()
258                 {
259                         return is_resource;
260                 }
261         
262                 public override string ToString () 
263                 {
264                         return name;
265                 }
266
267                 internal Guid MvId {
268                         get {
269                                 return Mono_GetGuid (this);
270                         }
271                 }
272
273 #if NET_2_0
274                 Guid ModuleVersionId {
275                         get {
276                                 return Mono_GetGuid (this);
277                         }
278                 }
279
280                 public void GetPEKind (out PortableExecutableKind peKind, out ImageFileMachine machine) {
281                         ModuleHandle.GetPEKind (out peKind, out machine);
282                 }
283 #endif
284                 
285
286 #if NET_2_0
287                 private Exception resolve_token_exception (int metadataToken, ResolveTokenError error, string tokenType) {
288                         if (error == ResolveTokenError.OutOfRange)
289                                 return new ArgumentOutOfRangeException ("metadataToken", String.Format ("Token 0x{0:x} is not valid in the scope of module {1}", metadataToken, name));
290                         else
291                                 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");
292                 }
293
294                 public FieldInfo ResolveField (int metadataToken) {
295                         ResolveTokenError error;
296
297                         IntPtr handle = ResolveFieldToken (_impl, metadataToken, out error);
298                         if (handle == IntPtr.Zero)
299                                 throw resolve_token_exception (metadataToken, error, "Field");
300                         else
301                                 return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
302                 }
303
304                 public MemberInfo ResolveMember (int metadataToken) {
305                         ResolveTokenError error;
306
307                         MemberInfo m = ResolveMemberToken (_impl, metadataToken, out error);
308                         if (m == null)
309                                 throw resolve_token_exception (metadataToken, error, "MemberInfo");
310                         else
311                                 return m;
312                 }
313
314                 public MethodBase ResolveMethod (int metadataToken) {
315                         ResolveTokenError error;
316
317                         IntPtr handle = ResolveMethodToken (_impl, metadataToken, out error);
318                         if (handle == IntPtr.Zero)
319                                 throw resolve_token_exception (metadataToken, error, "MethodBase");
320                         else
321                                 return MethodBase.GetMethodFromHandle (new RuntimeMethodHandle (handle));
322                 }
323
324                 public string ResolveString (int metadataToken) {
325                         ResolveTokenError error;
326
327                         string s = ResolveStringToken (_impl, metadataToken, out error);
328                         if (s == null)
329                                 throw resolve_token_exception (metadataToken, error, "string");
330                         else
331                                 return s;
332                 }
333
334                 public Type ResolveType (int metadataToken) {
335                         ResolveTokenError error;
336
337                         IntPtr handle = ResolveTypeToken (_impl, metadataToken, out error);
338                         if (handle == IntPtr.Zero)
339                                 throw resolve_token_exception (metadataToken, error, "Type");
340                         else
341                                 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
342                 }
343 #endif
344
345                 internal static Type MonoDebugger_ResolveType (Module module, int token)
346                 {
347                         ResolveTokenError error;
348
349                         IntPtr handle = ResolveTypeToken (module._impl, token, out error);
350                         if (handle == IntPtr.Zero)
351                                 return null;
352                         else
353                                 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
354                 }
355
356                 // Mono Extension: returns the GUID of this module
357                 internal static Guid Mono_GetGuid (Module module)
358                 {
359                         return new Guid (module.GetGuidInternal ());
360                 }
361
362                 private static bool filter_by_type_name (Type m, object filterCriteria) {
363                         string s = (string)filterCriteria;
364                         if (s.EndsWith ("*"))
365                                 return m.Name.StartsWith (s.Substring (0, s.Length - 1));
366                         else
367                                 return m.Name == s;
368                 }
369
370                 private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
371                         string s = (string)filterCriteria;
372                         if (s.EndsWith ("*"))
373                                 return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
374                         else
375                                 return String.Compare (m.Name, s, true) == 0;
376                 }
377
378                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
379                 private extern string GetGuidInternal ();
380
381                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
382                 private extern Type GetGlobalType ();
383
384                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
385                 internal static extern IntPtr ResolveTypeToken (IntPtr module, int token, out ResolveTokenError error);
386
387                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
388                 internal static extern IntPtr ResolveMethodToken (IntPtr module, int token, out ResolveTokenError error);
389
390                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
391                 internal static extern IntPtr ResolveFieldToken (IntPtr module, int token, out ResolveTokenError error);
392
393                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
394                 internal static extern string ResolveStringToken (IntPtr module, int token, out ResolveTokenError error);
395
396                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
397                 internal static extern MemberInfo ResolveMemberToken (IntPtr module, int token, out ResolveTokenError error);
398
399                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
400                 internal static extern void GetPEKind (IntPtr module, out PortableExecutableKind peKind, out ImageFileMachine machine);
401         }
402 }