6edaf0de37ca8173818d8258f5c5cd4f6d95792d
[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 using System.Runtime.Serialization;
38
39 namespace System.Reflection {
40
41         abstract class RuntimeModule : Module
42         {
43                 
44         }
45
46         [ComVisible (true)]
47         [ComDefaultInterfaceAttribute (typeof (_Module))]
48         [Serializable]
49         [ClassInterface(ClassInterfaceType.None)]
50         class MonoModule : RuntimeModule
51         {
52                 public
53                 override
54                 Assembly Assembly {
55                         get { return assembly; }
56                 }
57
58                 public
59                 override
60                 // Note: we do not ask for PathDiscovery because no path is returned here.
61                 // However MS Fx requires it (see FDBK23572 for details).
62                 string Name {
63                         get { return name; }
64                 }
65         
66                 public
67                 override
68                 string ScopeName {
69                         get { return scopename; }
70                 }
71
72                 public
73                 override
74                 int MDStreamVersion {
75                         get {
76                                 if (_impl == IntPtr.Zero)
77                                         throw new NotSupportedException ();
78                                 return GetMDStreamVersion (_impl);
79                         }
80                 }
81
82                 public
83                 override
84                 Guid ModuleVersionId {
85                         get {
86                                 return GetModuleVersionId ();
87                         }
88                 }
89
90                 public override
91                 string FullyQualifiedName {
92                         get {
93 #if !NET_2_1
94                                 if (SecurityManager.SecurityEnabled) {
95                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fqname).Demand ();
96                                 }
97 #endif
98                                 return fqname;
99                         }
100                 }
101
102                 public
103                 override
104                 bool IsResource()
105                 {
106                         return is_resource;
107                 }
108
109                 public override
110                 Type[] FindTypes(TypeFilter filter, object filterCriteria) 
111                 {
112                         var filtered = new List<Type> ();
113                         Type[] types = GetTypes ();
114                         foreach (Type t in types)
115                                 if (filter (t, filterCriteria))
116                                         filtered.Add (t);
117                         return filtered.ToArray ();
118                 }
119
120                 public override
121                 object[] GetCustomAttributes(bool inherit) 
122                 {
123                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
124                 }
125
126                 public override
127                 object[] GetCustomAttributes(Type attributeType, bool inherit) 
128                 {
129                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
130                 }
131
132                 public override
133                 FieldInfo GetField (string name, BindingFlags bindingAttr) 
134                 {
135                         if (name == null)
136                                 throw new ArgumentNullException("name");
137
138                         if (IsResource ())
139                                 return null;
140
141                         Type globalType = GetGlobalType ();
142                         return (globalType != null) ? globalType.GetField (name, bindingAttr) : null;
143                 }
144
145                 public override
146                 FieldInfo[] GetFields (BindingFlags bindingFlags)
147                 {
148                         if (IsResource ())
149                                 return new FieldInfo [0];
150
151                         Type globalType = GetGlobalType ();
152                         return (globalType != null) ? globalType.GetFields (bindingFlags) : new FieldInfo [0];
153                 }
154
155                 public override
156                 int MetadataToken {
157                         get { return get_MetadataToken (this); }
158                 }
159                 protected
160                 override
161                 MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
162                 {
163                         if (IsResource ())
164                                 return null;
165
166                         Type globalType = GetGlobalType ();
167                         if (globalType == null)
168                                 return null;
169                         if (types == null)
170                                 return globalType.GetMethod (name);
171                         return globalType.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
172                 }
173
174                 public
175                 override
176                 MethodInfo[] GetMethods (BindingFlags bindingFlags) {
177                         if (IsResource ())
178                                 return new MethodInfo [0];
179
180                         Type globalType = GetGlobalType ();
181                         return (globalType != null) ? globalType.GetMethods (bindingFlags) : new MethodInfo [0];
182                 }
183
184                 public override
185                 void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine) {
186                         ModuleHandle.GetPEKind (out peKind, out machine);
187                 }
188
189                 public override
190                 Type GetType(string className, bool throwOnError, bool ignoreCase) 
191                 {
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);
197                 }
198         
199                 public override
200                 bool IsDefined (Type attributeType, bool inherit) 
201                 {
202                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
203                 }
204
205                 public
206                 override
207                 FieldInfo ResolveField (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
208                         ResolveTokenError error;
209
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");
213                         else
214                                 return FieldInfo.GetFieldFromHandle (new RuntimeFieldHandle (handle));
215                 }
216
217                 public
218                 override
219                 MemberInfo ResolveMember (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
220
221                         ResolveTokenError error;
222
223                         MemberInfo m = ResolveMemberToken (_impl, metadataToken, ptrs_from_types (genericTypeArguments), ptrs_from_types (genericMethodArguments), out error);
224                         if (m == null)
225                                 throw resolve_token_exception (metadataToken, error, "MemberInfo");
226                         else
227                                 return m;
228                 }
229
230                 public
231                 override
232                 MethodBase ResolveMethod (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
233                         ResolveTokenError error;
234
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");
238                         else
239                                 return MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (handle));
240                 }
241
242                 public
243                 override
244                 string ResolveString (int metadataToken) {
245                         ResolveTokenError error;
246
247                         string s = ResolveStringToken (_impl, metadataToken, out error);
248                         if (s == null)
249                                 throw resolve_token_exception (metadataToken, error, "string");
250                         else
251                                 return s;
252                 }
253
254                 public
255                 override
256                 Type ResolveType (int metadataToken, Type [] genericTypeArguments, Type [] genericMethodArguments) {
257                         ResolveTokenError error;
258
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");
262                         else
263                                 return Type.GetTypeFromHandle (new RuntimeTypeHandle (handle));
264                 }
265
266                 public
267                 override
268                 byte[] ResolveSignature (int metadataToken) {
269                         ResolveTokenError error;
270
271                     byte[] res = ResolveSignature (_impl, metadataToken, out error);
272                         if (res == null)
273                                 throw resolve_token_exception (metadataToken, error, "signature");
274                         else
275                                 return res;
276                 }
277
278                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
279                 {
280                         if (info == null)
281                                 throw new ArgumentNullException ("info");
282
283                         UnitySerializationHolder.GetUnitySerializationInfo (info, UnitySerializationHolder.ModuleUnity, this.ScopeName, this.GetRuntimeAssembly ());
284                 }
285
286 #if !NET_2_1
287
288                 public
289                 override
290                 X509Certificate GetSignerCertificate ()
291                 {
292                         try {
293                                 return X509Certificate.CreateFromSignedFile (assembly.Location);
294                         }
295                         catch {
296                                 return null;
297                         }
298                 }
299 #endif
300
301                 public override
302                 Type[] GetTypes() 
303                 {
304                         return InternalGetTypes ();
305                 }
306
307                 public override IList<CustomAttributeData> GetCustomAttributesData () {
308                         return CustomAttributeData.GetCustomAttributes (this);
309                 }
310
311                 internal RuntimeAssembly GetRuntimeAssembly ()
312                 {
313                         return (RuntimeAssembly)assembly;
314                 }
315         }
316 }