Merge pull request #1631 from alexanderkyte/stringbuilder-referencesource
[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                         AssemblyName an = new AssemblyName(partialName);
143                         return LoadWithPartialNameInternal (an, securityEvidence, ref stackMark);
144                 }
145
146                 internal static RuntimeAssembly LoadWithPartialNameInternal (AssemblyName an, Evidence securityEvidence, ref StackCrawlMark stackMark)
147                 {
148                         throw new NotImplementedException ("LoadWithPartialNameInternal");
149                 }
150         }
151
152         [ComVisible (true)]
153         [ComDefaultInterfaceAttribute (typeof (_Assembly))]
154         [Serializable]
155         [ClassInterface(ClassInterfaceType.None)]
156         class MonoAssembly : RuntimeAssembly
157         {
158                 public
159                 override
160                 Type GetType (string name, bool throwOnError, bool ignoreCase)
161                 {
162                         Type res;
163                         if (name == null)
164                                 throw new ArgumentNullException (name);
165                         if (name.Length == 0)
166                         throw new ArgumentException ("name", "Name cannot be empty");
167
168                         res = InternalGetType (null, name, throwOnError, ignoreCase);
169                         return res;
170                 }
171
172                 public
173                 override
174                 Module GetModule (String name)
175                 {
176                         if (name == null)
177                                 throw new ArgumentNullException ("name");
178                         if (name.Length == 0)
179                                 throw new ArgumentException ("Name can't be empty");
180
181                         Module[] modules = GetModules (true);
182                         foreach (Module module in modules) {
183                                 if (module.ScopeName == name)
184                                         return module;
185                         }
186
187                         return null;
188                 }
189
190                 public
191                 override
192                 AssemblyName[] GetReferencedAssemblies () {
193                         return GetReferencedAssemblies (this);
194                 }
195
196                 public
197                 override
198                 Module[] GetModules (bool getResourceModules) {
199                         Module[] modules = GetModulesInternal ();
200
201                         if (!getResourceModules) {
202                                 var result = new List<Module> (modules.Length);
203                                 foreach (Module m in modules)
204                                         if (!m.IsResource ())
205                                                 result.Add (m);
206                                 return result.ToArray ();
207                         }
208                         else
209                                 return modules;
210                 }
211
212                 [MonoTODO ("Always returns the same as GetModules")]
213                 public
214                 override
215                 Module[] GetLoadedModules (bool getResourceModules)
216                 {
217                         return GetModules (getResourceModules);
218                 }
219
220                 public
221                 override
222                 Assembly GetSatelliteAssembly (CultureInfo culture)
223                 {
224                         return GetSatelliteAssembly (culture, null, true);
225                 }
226
227                 public
228                 override
229                 Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
230                 {
231                         return GetSatelliteAssembly (culture, version, true);
232                 }
233
234                 //FIXME remove GetManifestModule under v4, it's a v2 artifact
235                 [ComVisible (false)]
236                 public
237                 override
238                 Module ManifestModule {
239                         get {
240                                 return GetManifestModule ();
241                         }
242                 }
243
244                 public
245                 override
246                 bool GlobalAssemblyCache {
247                         get {
248                                 return get_global_assembly_cache ();
249                         }
250                 }
251         }
252 }
253
254