This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Reflection;
35 using System.Runtime.Serialization;
36 using System.Security.Cryptography.X509Certificates;
37 using System.Runtime.InteropServices;
38 using System.Runtime.CompilerServices;
39
40 namespace System.Reflection {
41
42         [Serializable]
43         public class Module : ISerializable, ICustomAttributeProvider {
44         
45                 public static readonly TypeFilter FilterTypeName;
46                 public static readonly TypeFilter FilterTypeNameIgnoreCase;
47         
48                 private IntPtr _impl; /* a pointer to a MonoImage */
49                 internal Assembly assembly;
50                 internal string fqname;
51                 internal string name;
52                 internal string scopename;
53                 internal bool is_resource;
54         
55                 const BindingFlags defaultBindingFlags = 
56                         BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
57                 
58                 static Module () {
59                         FilterTypeName = new TypeFilter (filter_by_type_name);
60                         FilterTypeNameIgnoreCase = new TypeFilter (filter_by_type_name_ignore_case);
61                 }
62
63                 internal Module () { }
64
65                 ~Module () {
66                         Close ();
67                 }
68         
69                 public Assembly Assembly {
70                         get { return assembly; }
71                 }
72         
73                 public virtual string FullyQualifiedName {
74                         get { return fqname; }
75                 }
76         
77                 public string Name {
78                         get { return name; }
79                 }
80         
81                 public string ScopeName {
82                         get { return scopename; }
83                 }
84         
85                 public virtual Type[] FindTypes(TypeFilter filter, object filterCriteria) 
86                 {
87                         System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
88                         Type[] types = GetTypes ();
89                         foreach (Type t in types)
90                                 if (filter (t, filterCriteria))
91                                         filtered.Add (t);
92                         return (Type[])filtered.ToArray (typeof(Type));
93                 }
94         
95                 public virtual object[] GetCustomAttributes(bool inherit) 
96                 {
97                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
98                 }
99         
100                 public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) 
101                 {
102                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
103                 }
104         
105                 public FieldInfo GetField (string name) 
106                 {
107                         if (IsResource ())
108                                 return null;
109
110                         return GetGlobalType ().GetField (name, BindingFlags.Public | BindingFlags.Static);
111                 }
112         
113                 public FieldInfo GetField (string name, BindingFlags flags) 
114                 {
115                         if (IsResource ())
116                                 return null;
117
118                         return GetGlobalType ().GetField (name, flags);
119                 }
120         
121                 public FieldInfo[] GetFields () 
122                 {
123                         if (IsResource ())
124                                 return new FieldInfo [0];
125
126                         return GetGlobalType ().GetFields (BindingFlags.Public | BindingFlags.Static);
127                 }
128         
129                 public MethodInfo GetMethod (string name) 
130                 {
131                         return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, Type.EmptyTypes, null);
132                 }
133         
134                 public MethodInfo GetMethod (string name, Type[] types) 
135                 {
136                         return GetMethodImpl (name, defaultBindingFlags, null, CallingConventions.Any, types, null);
137                 }
138         
139                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
140                 {
141                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
142                 }
143         
144                 protected virtual MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
145                 {
146                         if (IsResource ())
147                                 return null;
148
149                         return GetGlobalType ().GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
150                 }
151         
152                 public MethodInfo[] GetMethods () 
153                 {
154                         if (IsResource ())
155                                 return new MethodInfo [0];
156
157                         return GetGlobalType ().GetMethods ();
158                 }
159         
160                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context) 
161                 {
162                         UnitySerializationHolder.GetModuleData (this, info, context);
163                 }
164         
165                 public X509Certificate GetSignerCertificate ()
166                 {
167                         try {
168                                 return X509Certificate.CreateFromSignedFile (assembly.Location);
169                         }
170                         catch {
171                                 return null;
172                         }
173                 }
174         
175                 public virtual Type GetType(string className) 
176                 {
177                         return GetType (className, false, false);
178                 }
179         
180                 public virtual Type GetType(string className, bool ignoreCase) 
181                 {
182                         return GetType (className, false, ignoreCase);
183                 }
184         
185                 public virtual Type GetType(string className, bool throwOnError, bool ignoreCase) 
186                 {
187                         if (className == null)
188                                 throw new ArgumentNullException ("className");
189                         if (className == String.Empty)
190                                 throw new ArgumentException ("Type name can't be empty");
191                         return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
192                 }
193
194                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
195                 private extern Type[] InternalGetTypes ();
196         
197                 public virtual Type[] GetTypes() 
198                 {
199                         return InternalGetTypes ();
200                 }
201         
202                 public virtual bool IsDefined (Type attributeType, bool inherit) 
203                 {
204                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
205                 }
206         
207                 public bool IsResource()
208                 {
209                         return is_resource;
210                 }
211         
212                 public override string ToString () 
213                 {
214                         return name;
215                 }
216
217                 // Mono Extension: returns the GUID of this module
218                 internal Guid Mono_GetGuid ()
219                 {
220                         return new Guid (GetGuidInternal ());
221                 }
222
223                 private static bool filter_by_type_name (Type m, object filterCriteria) {
224                         string s = (string)filterCriteria;
225                         if (s.EndsWith ("*"))
226                                 return m.Name.StartsWith (s.Substring (0, s.Length - 1));
227                         else
228                                 return m.Name == s;
229                 }
230
231                 private static bool filter_by_type_name_ignore_case (Type m, object filterCriteria) {
232                         string s = (string)filterCriteria;
233                         if (s.EndsWith ("*"))
234                                 return m.Name.ToLower ().StartsWith (s.Substring (0, s.Length - 1).ToLower ());
235                         else
236                                 return String.Compare (m.Name, s, true) == 0;
237                 }
238
239                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
240                 private extern string GetGuidInternal ();
241
242                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
243                 private extern Type GetGlobalType ();
244
245                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246                 private extern void Close ();
247         }
248 }