2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Reflection / MonoModule.cs
blob90b13cfda018aad6c6ce5a9f2addb4d225d714ef
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.
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;
39 namespace System.Reflection {
41 #if NET_4_0
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
51 public
52 #if NET_4_0
53 override
54 #endif
55 Assembly Assembly {
56 get { return assembly; }
59 public
60 #if NET_4_0
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; }
69 public
70 #if NET_4_0
71 override
72 #endif
73 string ScopeName {
74 get { return scopename; }
77 public
78 #if NET_4_0
79 override
80 #endif
81 int MDStreamVersion {
82 get {
83 if (_impl == IntPtr.Zero)
84 throw new NotSupportedException ();
85 return GetMDStreamVersion (_impl);
89 public
90 #if NET_4_0
91 override
92 #endif
93 Guid ModuleVersionId {
94 get {
95 return GetModuleVersionId ();
99 #if NET_4_0
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 ();
110 #endif
111 return fqname;
115 public
116 #if NET_4_0
117 override
118 #endif
119 bool IsResource()
121 return is_resource;
124 #if NET_4_0
125 public override
126 #else
127 public virtual
128 #endif
129 Type[] FindTypes(TypeFilter filter, object filterCriteria)
131 System.Collections.ArrayList filtered = new System.Collections.ArrayList ();
132 Type[] types = GetTypes ();
133 foreach (Type t in types)
134 if (filter (t, filterCriteria))
135 filtered.Add (t);
136 return (Type[])filtered.ToArray (typeof(Type));
139 #if NET_4_0
140 public override
141 #else
142 public virtual
143 #endif
144 object[] GetCustomAttributes(bool inherit)
146 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
149 #if NET_4_0
150 public override
151 #else
152 public virtual
153 #endif
154 object[] GetCustomAttributes(Type attributeType, bool inherit)
156 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
159 #if NET_4_0
160 public override
161 #else
162 public virtual
163 #endif
164 FieldInfo GetField (string name, BindingFlags bindingAttr)
166 if (IsResource ())
167 return null;
169 Type globalType = GetGlobalType ();
170 return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
173 #if NET_4_0
174 public override
175 #else
176 public virtual
177 #endif
178 FieldInfo[] GetFields (BindingFlags bindingFlags)
180 if (IsResource ())
181 return new FieldInfo [0];
183 Type globalType = GetGlobalType ();
184 return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
187 #if NET_4_0
188 public override
189 #else
190 public virtual
191 #endif
192 int MetadataToken {
193 get { return get_MetadataToken (this); }
195 protected
196 #if NET_4_0
197 override
198 #else
199 virtual
200 #endif
201 MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
203 if (IsResource ())
204 return null;
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);
214 public
215 #if NET_4_0
216 override
217 #endif
218 MethodInfo[] GetMethods (BindingFlags bindingFlags) {
219 if (IsResource ())
220 return new MethodInfo [0];
222 Type globalType = GetGlobalType ();
223 return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
226 #if NET_4_0
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);
235 #if NET_4_0
236 public override
237 #else
238 public virtual
239 #endif
240 Type GetType(string className, bool throwOnError, bool ignoreCase)
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);
249 #if NET_4_0
250 public override
251 #else
252 public virtual
253 #endif
254 bool IsDefined (Type attributeType, bool inherit)
256 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
259 public
260 #if NET_4_0
261 override
262 #endif
263 FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
264 ResolveTokenError error;
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));
273 public
274 #if NET_4_0
275 override
276 #endif
277 MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
279 ResolveTokenError error;
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;
288 public
289 #if NET_4_0
290 override
291 #endif
292 MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
293 ResolveTokenError error;
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));
302 public
303 #if NET_4_0
304 override
305 #endif
306 string ResolveString (int metadataToken) {
307 ResolveTokenError error;
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;
316 public
317 #if NET_4_0
318 override
319 #endif
320 Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
321 ResolveTokenError error;
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));
330 public
331 #if NET_4_0
332 override
333 #endif
334 byte[] ResolveSignature (int metadataToken) {
335 ResolveTokenError error;
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;
344 #if !NET_2_1
346 public
347 #if NET_4_0
348 override
349 #endif
350 X509Certificate GetSignerCertificate ()
352 try {
353 return X509Certificate.CreateFromSignedFile (assembly.Location);
355 catch {
356 return null;
359 #endif
361 #if NET_4_0
362 public override
363 #else
364 public virtual
365 #endif
366 Type[] GetTypes()
368 return InternalGetTypes ();
371 #if NET_4_0
372 public override IList<CustomAttributeData> GetCustomAttributesData () {
373 return CustomAttributeData.GetCustomAttributes (this);
375 #endif