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 {
41 abstract class RuntimeModule : Module
47 [ComDefaultInterfaceAttribute (typeof (_Module))]
49 [ClassInterface(ClassInterfaceType.None)]
50 class MonoModule : RuntimeModule
55 get { return assembly; }
60 // Note: we do not ask for PathDiscovery because no path is returned here.
61 // However MS Fx requires it (see FDBK23572 for details).
69 get { return scopename; }
76 if (_impl == IntPtr.Zero)
77 throw new NotSupportedException ();
78 return GetMDStreamVersion (_impl);
84 Guid ModuleVersionId {
86 return GetModuleVersionId ();
91 string FullyQualifiedName {
94 if (SecurityManager.SecurityEnabled) {
95 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
110 Type[] FindTypes(TypeFilter filter, object filterCriteria)
112 var filtered = new List<Type> ();
113 Type[] types = GetTypes ();
114 foreach (Type t in types)
115 if (filter (t, filterCriteria))
117 return filtered.ToArray ();
121 object[] GetCustomAttributes(bool inherit)
123 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
127 object[] GetCustomAttributes(Type attributeType, bool inherit)
129 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
133 FieldInfo GetField (string name, BindingFlags bindingAttr)
136 throw new ArgumentNullException("name");
141 Type globalType = GetGlobalType ();
142 return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
146 FieldInfo[] GetFields (BindingFlags bindingFlags)
149 return new FieldInfo [0];
151 Type globalType = GetGlobalType ();
152 return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
157 get { return get_MetadataToken (this); }
161 MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
166 Type globalType = GetGlobalType ();
167 if (globalType == null)
170 return globalType.GetMethod (name);
171 return globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
176 MethodInfo[] GetMethods (BindingFlags bindingFlags) {
178 return new MethodInfo [0];
180 Type globalType = GetGlobalType ();
181 return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
185 void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
186 ModuleHandle.GetPEKind (out peKind, out machine);
190 Type GetType(string className, bool throwOnError, bool ignoreCase)
192 if (className == null)
193 throw new ArgumentNullException ("className");
194 if (className == String.Empty)
195 throw new ArgumentException ("Type name can't be empty");
196 return assembly.InternalGetType (this, className, throwOnError, ignoreCase);
200 bool IsDefined (Type attributeType, bool inherit)
202 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
207 FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
208 ResolveTokenError error;
210 IntPtr handle = ResolveFieldToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
211 if (handle == IntPtr.Zero)
212 throw resolve_token_exception (metadataToken, error, "Field");
214 return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
219 MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
221 ResolveTokenError error;
223 MemberInfo m = ResolveMemberToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
225 throw resolve_token_exception (metadataToken, error, "MemberInfo");
232 MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
233 ResolveTokenError error;
235 IntPtr handle = ResolveMethodToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
236 if (handle == IntPtr.Zero)
237 throw resolve_token_exception (metadataToken, error, "MethodBase");
239 return MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
244 string ResolveString (int metadataToken) {
245 ResolveTokenError error;
247 string s = ResolveStringToken (_impl, metadataToken, out error);
249 throw resolve_token_exception (metadataToken, error, "string");
256 Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
257 ResolveTokenError error;
259 IntPtr handle = ResolveTypeToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
260 if (handle == IntPtr.Zero)
261 throw resolve_token_exception (metadataToken, error, "Type");
263 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
268 byte[] ResolveSignature (int metadataToken) {
269 ResolveTokenError error;
271 byte[] res = ResolveSignature (_impl, metadataToken, out error);
273 throw resolve_token_exception (metadataToken, error, "signature");
278 public override void GetObjectData (SerializationInfo info, StreamingContext context)
281 throw new ArgumentNullException ("info");
283 UnitySerializationHolder.GetUnitySerializationInfo (info, UnitySerializationHolder.ModuleUnity, this.ScopeName, this.GetRuntimeAssembly ());
290 X509Certificate GetSignerCertificate ()
293 return X509Certificate.CreateFromSignedFile (assembly.Location);
304 return InternalGetTypes ();
307 public override IList<CustomAttributeData> GetCustomAttributesData () {
308 return CustomAttributeData.GetCustomAttributes (this);
311 internal RuntimeAssembly GetRuntimeAssembly ()
313 return (RuntimeAssembly)assembly;