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;
37 using System.Runtime.Serialization;
39 namespace System.Reflection {
42 [ComDefaultInterfaceAttribute (typeof (_Module))]
44 [ClassInterface(ClassInterfaceType.None)]
45 class MonoModule : Module {
50 get { return assembly; }
55 // Note: we do not ask for PathDiscovery because no path is returned here.
56 // However MS Fx requires it (see FDBK23572 for details).
64 get { return scopename; }
71 if (_impl == IntPtr.Zero)
72 throw new NotSupportedException ();
73 return GetMDStreamVersion (_impl);
79 Guid ModuleVersionId {
81 return GetModuleVersionId ();
86 string FullyQualifiedName {
89 if (SecurityManager.SecurityEnabled) {
90 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
105 Type[] FindTypes(TypeFilter filter, object filterCriteria)
107 var filtered = new List<Type> ();
108 Type[] types = GetTypes ();
109 foreach (Type t in types)
110 if (filter (t, filterCriteria))
112 return filtered.ToArray ();
116 object[] GetCustomAttributes(bool inherit)
118 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
122 object[] GetCustomAttributes(Type attributeType, bool inherit)
124 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
128 FieldInfo GetField (string name, BindingFlags bindingAttr)
133 Type globalType = GetGlobalType ();
134 return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
138 FieldInfo[] GetFields (BindingFlags bindingFlags)
141 return new FieldInfo [0];
143 Type globalType = GetGlobalType ();
144 return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
149 get { return get_MetadataToken (this); }
153 MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
158 Type globalType = GetGlobalType ();
159 if (globalType == null)
162 return globalType.GetMethod (name);
163 return globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
168 MethodInfo[] GetMethods (BindingFlags bindingFlags) {
170 return new MethodInfo [0];
172 Type globalType = GetGlobalType ();
173 return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
177 void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
178 ModuleHandle.GetPEKind (out peKind, out machine);
182 Type GetType(string className, bool throwOnError, bool ignoreCase)
184 if (className == null)
185 throw new ArgumentNullException ("className");
186 if (className == String.Empty)
187 throw new ArgumentException ("Type name can't be empty");
188 return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
192 bool IsDefined (Type attributeType, bool inherit)
194 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
199 FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
200 ResolveTokenError error;
202 IntPtr handle = ResolveFieldToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
203 if (handle == IntPtr.Zero)
204 throw resolve_token_exception (metadataToken, error, "Field");
206 return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
211 MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
213 ResolveTokenError error;
215 MemberInfo m = ResolveMemberToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
217 throw resolve_token_exception (metadataToken, error, "MemberInfo");
224 MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
225 ResolveTokenError error;
227 IntPtr handle = ResolveMethodToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
228 if (handle == IntPtr.Zero)
229 throw resolve_token_exception (metadataToken, error, "MethodBase");
231 return MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
236 string ResolveString (int metadataToken) {
237 ResolveTokenError error;
239 string s = ResolveStringToken (_impl, metadataToken, out error);
241 throw resolve_token_exception (metadataToken, error, "string");
248 Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
249 ResolveTokenError error;
251 IntPtr handle = ResolveTypeToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
252 if (handle == IntPtr.Zero)
253 throw resolve_token_exception (metadataToken, error, "Type");
255 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
260 byte[] ResolveSignature (int metadataToken) {
261 ResolveTokenError error;
263 byte[] res = ResolveSignature (_impl, metadataToken, out error);
265 throw resolve_token_exception (metadataToken, error, "signature");
270 public override void GetObjectData (SerializationInfo info, StreamingContext context)
273 throw new ArgumentNullException ("info");
275 UnitySerializationHolder.GetUnitySerializationInfo (info, UnitySerializationHolder.ModuleUnity, this.ScopeName, this.GetRuntimeAssembly ());
282 X509Certificate GetSignerCertificate ()
285 return X509Certificate.CreateFromSignedFile (assembly.Location);
296 return InternalGetTypes ();
299 public override IList<CustomAttributeData> GetCustomAttributesData () {
300 return CustomAttributeData.GetCustomAttributes (this);
303 internal RuntimeAssembly GetRuntimeAssembly ()
305 return (RuntimeAssembly)assembly;