Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / corlib / System.Reflection / MonoModule.cs
1 //
2 // System.Reflection/MonoModule.cs
3 //
4 // Author:
5 //   Rodrigo Kumpera (rkumpera@novell.com)
6 //
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Globalization;
33 using System.Runtime.InteropServices;
34 using System.Security.Cryptography.X509Certificates;
35 using System.Security;
36 using System.Security.Permissions;
37
38
39 namespace System.Reflection {
40
41 #if NET_4_0 || MOONLIGHT || MOBILE
42         [ComVisible (true)]
43         [ComDefaultInterfaceAttribute (typeof (_Module))]
44         [Serializable]
45         [ClassInterface(ClassInterfaceType.None)]
46         class MonoModule : Module {
47 #else
48         public partial class Module {
49 #endif
50
51                 public
52 #if NET_4_0 || MOONLIGHT || MOBILE
53                 override
54 #endif
55                 Assembly Assembly {
56                         get { return assembly; }
57                 }
58
59                 public
60 #if NET_4_0 || MOONLIGHT || MOBILE
61                 override
62 #endif
63                 // Note: we do not ask for PathDiscovery because no path is returned here.
64                 // However MS Fx requires it (see FDBK23572 for details).
65                 string Name {
66                         get { return name; }
67                 }
68         
69                 public
70 #if NET_4_0 || MOONLIGHT || MOBILE
71                 override
72 #endif
73                 string ScopeName {
74                         get { return scopename; }
75                 }
76
77                 public
78 #if NET_4_0 || MOONLIGHT || MOBILE
79                 override
80 #endif
81                 int MDStreamVersion {
82                         get {
83                                 if (_impl == IntPtr.Zero)
84                                         throw new NotSupportedException ();
85                                 return GetMDStreamVersion (_impl);
86                         }
87                 }
88
89                 public
90 #if NET_4_0 || MOONLIGHT || MOBILE
91                 override
92 #endif
93                 Guid ModuleVersionId {
94                         get {
95                                 return GetModuleVersionId ();
96                         }
97                 }
98
99 #if NET_4_0 || MOONLIGHT || MOBILE
100                 public override
101 #else
102                 public virtual
103 #endif
104                 string FullyQualifiedName {
105                         get {
106 #if !NET_2_1
107                                 if (SecurityManager.SecurityEnabled) {
108                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
109                                 }
110 #endif
111                                 return fqname;
112                         }
113                 }
114
115                 public
116 #if NET_4_0 || MOONLIGHT || MOBILE
117                 override
118 #endif
119                 bool IsResource()
120                 {
121                         return is_resource;
122                 }
123
124 #if NET_4_0 || MOONLIGHT || MOBILE
125                 public override
126 #else
127                 public virtual
128 #endif
129                 Type[] FindTypes(TypeFilter filter, object filterCriteria) 
130                 {
131                         var filtered = new List<Type> ();
132                         Type[] types = GetTypes ();
133                         foreach (Type t in types)
134                                 if (filter (t, filterCriteria))
135                                         filtered.Add (t);
136                         return filtered.ToArray ();
137                 }
138
139 #if NET_4_0 || MOONLIGHT || MOBILE
140                 public override
141 #else
142                 public virtual
143 #endif
144                 object[] GetCustomAttributes(bool inherit) 
145                 {
146                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
147                 }
148
149 #if NET_4_0 || MOONLIGHT || MOBILE
150                 public override
151 #else
152                 public virtual
153 #endif
154                 object[] GetCustomAttributes(Type attributeType, bool inherit) 
155                 {
156                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
157                 }
158
159 #if NET_4_0 || MOONLIGHT || MOBILE
160                 public override
161 #else
162                 public virtual
163 #endif
164                 FieldInfo GetField (string name, BindingFlags bindingAttr) 
165                 {
166                         if (IsResource ())
167                                 return null;
168
169                         Type globalType = GetGlobalType ();
170                         return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
171                 }
172
173 #if NET_4_0 || MOONLIGHT || MOBILE
174                 public override
175 #else
176                 public virtual
177 #endif
178                 FieldInfo[] GetFields (BindingFlags bindingFlags)
179                 {
180                         if (IsResource ())
181                                 return new FieldInfo [0];
182
183                         Type globalType = GetGlobalType ();
184                         return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
185                 }
186
187 #if NET_4_0 || MOONLIGHT || MOBILE
188                 public override
189 #else
190                 public virtual
191 #endif
192                 int MetadataToken {
193                         get { return get_MetadataToken (this); }
194                 }
195                 protected
196 #if NET_4_0 || MOONLIGHT || MOBILE
197                 override
198 #else
199                 virtual
200 #endif  
201                 MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
202                 {
203                         if (IsResource ())
204                                 return null;
205
206                         Type globalType = GetGlobalType ();
207                         if (globalType == null)
208                                 return null;
209                         if (types == null)
210                                 return globalType.GetMethod (name);
211                         return globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
212                 }
213
214                 public
215 #if NET_4_0 || MOONLIGHT || MOBILE
216                 override
217 #endif
218                 MethodInfo[] GetMethods (BindingFlags bindingFlags) {
219                         if (IsResource ())
220                                 return new MethodInfo [0];
221
222                         Type globalType = GetGlobalType ();
223                         return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
224                 }
225
226 #if NET_4_0 || MOONLIGHT || MOBILE
227                 public override
228 #else
229                 public virtual
230 #endif
231                 void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
232                         ModuleHandle.GetPEKind (out peKind, out machine);
233                 }
234
235 #if NET_4_0 || MOONLIGHT || MOBILE
236                 public override
237 #else
238                 public virtual
239 #endif
240                 Type GetType(string className, bool throwOnError, bool ignoreCase) 
241                 {
242                         if (className == null)
243                                 throw new ArgumentNullException ("className");
244                         if (className == String.Empty)
245                                 throw new ArgumentException ("Type name can't be empty");
246                         return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
247                 }
248         
249 #if NET_4_0 || MOONLIGHT || MOBILE
250                 public override
251 #else
252                 public virtual
253 #endif
254                 bool IsDefined (Type attributeType, bool inherit) 
255                 {
256                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
257                 }
258
259                 public
260 #if NET_4_0 || MOONLIGHT || MOBILE
261                 override
262 #endif
263                 FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
264                         ResolveTokenError error;
265
266                         IntPtr handle = ResolveFieldToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
267                         if (handle == IntPtr.Zero)
268                                 throw resolve_token_exception (metadataToken, error, "Field");
269                         else
270                                 return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
271                 }
272
273                 public
274 #if NET_4_0 || MOONLIGHT || MOBILE
275                 override
276 #endif
277                 MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
278
279                         ResolveTokenError error;
280
281                         MemberInfo m = ResolveMemberToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
282                         if (m == null)
283                                 throw resolve_token_exception (metadataToken, error, "MemberInfo");
284                         else
285                                 return m;
286                 }
287
288                 public
289 #if NET_4_0 || MOONLIGHT || MOBILE
290                 override
291 #endif
292                 MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
293                         ResolveTokenError error;
294
295                         IntPtr handle = ResolveMethodToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
296                         if (handle == IntPtr.Zero)
297                                 throw resolve_token_exception (metadataToken, error, "MethodBase");
298                         else
299                                 return MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
300                 }
301
302                 public
303 #if NET_4_0 || MOONLIGHT || MOBILE
304                 override
305 #endif
306                 string ResolveString (int metadataToken) {
307                         ResolveTokenError error;
308
309                         string s = ResolveStringToken (_impl, metadataToken, out error);
310                         if (s == null)
311                                 throw resolve_token_exception (metadataToken, error, "string");
312                         else
313                                 return s;
314                 }
315
316                 public
317 #if NET_4_0 || MOONLIGHT || MOBILE
318                 override
319 #endif
320                 Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
321                         ResolveTokenError error;
322
323                         IntPtr handle = ResolveTypeToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
324                         if (handle == IntPtr.Zero)
325                                 throw resolve_token_exception (metadataToken, error, "Type");
326                         else
327                                 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
328                 }
329
330                 public
331 #if NET_4_0 || MOONLIGHT || MOBILE
332                 override
333 #endif
334                 byte[] ResolveSignature (int metadataToken) {
335                         ResolveTokenError error;
336
337                     byte[] res = ResolveSignature (_impl, metadataToken, out error);
338                         if (res == null)
339                                 throw resolve_token_exception (metadataToken, error, "signature");
340                         else
341                                 return res;
342                 }
343
344 #if !NET_2_1
345
346                 public
347 #if NET_4_0
348                 override
349 #endif
350                 X509Certificate GetSignerCertificate ()
351                 {
352                         try {
353                                 return X509Certificate.CreateFromSignedFile (assembly.Location);
354                         }
355                         catch {
356                                 return null;
357                         }
358                 }
359 #endif
360
361 #if NET_4_0 || MOONLIGHT || MOBILE
362                 public override
363 #else
364                 public virtual
365 #endif
366                 Type[] GetTypes() 
367                 {
368                         return InternalGetTypes ();
369                 }
370
371 #if NET_4_0 || MOONLIGHT || MOBILE
372                 public override IList<CustomAttributeData> GetCustomAttributesData () {
373                         return CustomAttributeData.GetCustomAttributes (this);
374                 }
375 #endif
376         }
377 }