[runtime] Replace pedump boehm dependency with sgen dependency
[mono.git] / mcs / class / corlib / System.Reflection / MonoAssembly.cs
1 //
2 // System.Reflection/MonoAssembly.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.Globalization;
32 using System.Runtime.InteropServices;
33 #if !FULL_AOT_RUNTIME
34 using System.Reflection.Emit;
35 #endif
36 using System.Collections.Generic;
37 using System.Runtime.Serialization;
38 using System.Threading;
39 using System.Diagnostics.Contracts;
40 using System.Security.Policy;
41 using System.Security.Permissions;
42
43 namespace System.Reflection {
44
45         abstract class RuntimeAssembly : Assembly
46         {
47                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
48                 {
49                         if (info == null)
50                                 throw new ArgumentNullException ("info");
51
52                         UnitySerializationHolder.GetUnitySerializationInfo (info,
53                                                                UnitySerializationHolder.AssemblyUnity,
54                                                                this.FullName,
55                                                                this);
56                 }
57
58                 internal static RuntimeAssembly GetExecutingAssembly (ref StackCrawlMark stackMark)
59                 {
60                         // Mono runtime does not support StackCrawlMark, The easiest workaround is to replace use
61                         // of StackCrawlMark.LookForMyCaller with GetCallingAssembly
62                         throw new NotSupportedException ();
63                 }
64
65         // Creates AssemblyName. Fills assembly if AssemblyResolve event has been raised.
66         [System.Security.SecurityCritical]  // auto-generated
67         internal static AssemblyName CreateAssemblyName(
68             String assemblyString, 
69             bool forIntrospection, 
70             out RuntimeAssembly assemblyFromResolveEvent)
71         {
72             if (assemblyString == null)
73                 throw new ArgumentNullException("assemblyString");
74             Contract.EndContractBlock();
75
76             if ((assemblyString.Length == 0) ||
77                 (assemblyString[0] == '\0'))
78                 throw new ArgumentException(Environment.GetResourceString("Format_StringZeroLength"));
79
80             if (forIntrospection)
81                 AppDomain.CheckReflectionOnlyLoadSupported();
82
83             AssemblyName an = new AssemblyName();
84
85             an.Name = assemblyString;
86             assemblyFromResolveEvent = null; // instead of an.nInit(out assemblyFromResolveEvent, forIntrospection, true);
87             return an;
88         }
89
90         internal static RuntimeAssembly InternalLoadAssemblyName(
91             AssemblyName assemblyRef, 
92             Evidence assemblySecurity,
93             RuntimeAssembly reqAssembly,
94             ref StackCrawlMark stackMark,
95 #if FEATURE_HOSTED_BINDER
96             IntPtr pPrivHostBinder,
97 #endif
98             bool throwOnFileNotFound, 
99             bool forIntrospection,
100             bool suppressSecurityChecks)
101         {
102             if (assemblyRef == null)
103                 throw new ArgumentNullException("assemblyRef");
104             Contract.EndContractBlock();
105
106             if (assemblyRef.CodeBase != null)
107             {
108                 AppDomain.CheckLoadFromSupported();
109             }
110
111             assemblyRef = (AssemblyName)assemblyRef.Clone();
112 #if FEATURE_VERSIONING
113             if (!forIntrospection &&
114                 (assemblyRef.ProcessorArchitecture != ProcessorArchitecture.None)) {
115                 // PA does not have a semantics for by-name binds for execution
116                 assemblyRef.ProcessorArchitecture = ProcessorArchitecture.None;
117             }
118 #endif
119
120             if (assemblySecurity != null)
121             {
122 #if FEATURE_CAS_POLICY
123                 if (!AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
124                 {
125                     throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
126                 }
127 #endif // FEATURE_CAS_POLICY
128
129                 if (!suppressSecurityChecks)
130                 {
131 #pragma warning disable 618
132                     new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Demand();
133 #pragma warning restore 618
134                 }
135             }
136
137                         return (RuntimeAssembly) Assembly.Load (assemblyRef);
138                 }
139
140                 internal static RuntimeAssembly LoadWithPartialNameInternal (String partialName, Evidence securityEvidence, ref StackCrawlMark stackMark)
141                 {
142                         // Mono runtime does not support StackCrawlMark
143                         //FIXME stackMark should probably change method behavior in some cases.
144                         return (RuntimeAssembly) Assembly.LoadWithPartialName (partialName, securityEvidence);
145                 }
146
147                 internal static RuntimeAssembly LoadWithPartialNameInternal (AssemblyName an, Evidence securityEvidence, ref StackCrawlMark stackMark)
148                 {
149                         return LoadWithPartialNameInternal (an.ToString (), securityEvidence, ref stackMark);
150                 }
151
152         }
153
154         [ComVisible (true)]
155         [ComDefaultInterfaceAttribute (typeof (_Assembly))]
156         [Serializable]
157         [ClassInterface(ClassInterfaceType.None)]
158         class MonoAssembly : RuntimeAssembly
159         {
160                 public
161                 override
162                 Type GetType (string name, bool throwOnError, bool ignoreCase)
163                 {
164                         Type res;
165                         if (name == null)
166                                 throw new ArgumentNullException (name);
167                         if (name.Length == 0)
168                         throw new ArgumentException ("name", "Name cannot be empty");
169
170                         res = InternalGetType (null, name, throwOnError, ignoreCase);
171                         return res;
172                 }
173
174                 public
175                 override
176                 Module GetModule (String name)
177                 {
178                         if (name == null)
179                                 throw new ArgumentNullException ("name");
180                         if (name.Length == 0)
181                                 throw new ArgumentException ("Name can't be empty");
182
183                         Module[] modules = GetModules (true);
184                         foreach (Module module in modules) {
185                                 if (module.ScopeName == name)
186                                         return module;
187                         }
188
189                         return null;
190                 }
191
192                 public
193                 override
194                 AssemblyName[] GetReferencedAssemblies () {
195                         return GetReferencedAssemblies (this);
196                 }
197
198                 public
199                 override
200                 Module[] GetModules (bool getResourceModules) {
201                         Module[] modules = GetModulesInternal ();
202
203                         if (!getResourceModules) {
204                                 var result = new List<Module> (modules.Length);
205                                 foreach (Module m in modules)
206                                         if (!m.IsResource ())
207                                                 result.Add (m);
208                                 return result.ToArray ();
209                         }
210                         else
211                                 return modules;
212                 }
213
214                 [MonoTODO ("Always returns the same as GetModules")]
215                 public
216                 override
217                 Module[] GetLoadedModules (bool getResourceModules)
218                 {
219                         return GetModules (getResourceModules);
220                 }
221
222                 public
223                 override
224                 Assembly GetSatelliteAssembly (CultureInfo culture)
225                 {
226                         return GetSatelliteAssembly (culture, null, true);
227                 }
228
229                 public
230                 override
231                 Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
232                 {
233                         return GetSatelliteAssembly (culture, version, true);
234                 }
235
236                 //FIXME remove GetManifestModule under v4, it's a v2 artifact
237                 [ComVisible (false)]
238                 public
239                 override
240                 Module ManifestModule {
241                         get {
242                                 return GetManifestModule ();
243                         }
244                 }
245
246                 public
247                 override
248                 bool GlobalAssemblyCache {
249                         get {
250                                 return get_global_assembly_cache ();
251                         }
252                 }
253         }
254 }
255
256