2 // System.Reflection/MonoModule.cs
5 // Rodrigo Kumpera (rkumpera@novell.com)
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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 {
43 [ComDefaultInterfaceAttribute (typeof (_Module))]
45 [ClassInterface(ClassInterfaceType.None)]
46 class MonoModule : Module {
48 public partial class Module {
56 get { return assembly; }
63 // Note: we do not ask for PathDiscovery because no path is returned here.
64 // However MS Fx requires it (see FDBK23572 for details).
74 get { return scopename; }
83 if (_impl == IntPtr.Zero)
84 throw new NotSupportedException ();
85 return GetMDStreamVersion (_impl);
93 Guid ModuleVersionId {
95 return GetModuleVersionId ();
104 string FullyQualifiedName {
107 if (SecurityManager.SecurityEnabled) {
108 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
129 Type[] FindTypes(TypeFilter filter, object filterCriteria)
131 var filtered = new List<Type> ();
132 Type[] types = GetTypes ();
133 foreach (Type t in types)
134 if (filter (t, filterCriteria))
136 return filtered.ToArray ();
144 object[] GetCustomAttributes(bool inherit)
146 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
154 object[] GetCustomAttributes(Type attributeType, bool inherit)
156 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
164 FieldInfo GetField (string name, BindingFlags bindingAttr)
169 Type globalType = GetGlobalType ();
170 return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
178 FieldInfo[] GetFields (BindingFlags bindingFlags)
181 return new FieldInfo [0];
183 Type globalType = GetGlobalType ();
184 return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
193 get { return get_MetadataToken (this); }
201 MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
206 Type globalType = GetGlobalType ();
207 if (globalType == null)
210 return globalType.GetMethod (name);
211 return globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
218 MethodInfo[] GetMethods (BindingFlags bindingFlags) {
220 return new MethodInfo [0];
222 Type globalType = GetGlobalType ();
223 return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
231 void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
232 ModuleHandle.GetPEKind (out peKind, out machine);
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);
254 bool IsDefined (Type attributeType, bool inherit)
256 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
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");
270 return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
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);
283 throw resolve_token_exception (metadataToken, error, "MemberInfo");
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");
299 return MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
306 string ResolveString (int metadataToken) {
307 ResolveTokenError error;
309 string s = ResolveStringToken (_impl, metadataToken, out error);
311 throw resolve_token_exception (metadataToken, error, "string");
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");
327 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
334 byte[] ResolveSignature (int metadataToken) {
335 ResolveTokenError error;
337 byte[] res = ResolveSignature (_impl, metadataToken, out error);
339 throw resolve_token_exception (metadataToken, error, "signature");
350 X509Certificate GetSignerCertificate ()
353 return X509Certificate.CreateFromSignedFile (assembly.Location);
368 return InternalGetTypes ();
372 public override IList<CustomAttributeData> GetCustomAttributesData () {
373 return CustomAttributeData.GetCustomAttributes (this);