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