2 // System.Reflection/MonoAssembly.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.Globalization;
32 using System.Runtime.InteropServices;
34 using System.Reflection.Emit;
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;
43 namespace System.Reflection {
45 abstract class RuntimeAssembly : Assembly
47 public override void GetObjectData (SerializationInfo info, StreamingContext context)
50 throw new ArgumentNullException ("info");
52 UnitySerializationHolder.GetUnitySerializationInfo (info,
53 UnitySerializationHolder.AssemblyUnity,
58 internal static RuntimeAssembly GetExecutingAssembly (ref StackCrawlMark stackMark)
60 // Mono runtime does not support StackCrawlMark, The easiest workaround is to replace use
61 // of StackCrawlMark.LookForMyCaller with GetCallingAssembly
62 throw new NotSupportedException ();
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)
72 if (assemblyString == null)
73 throw new ArgumentNullException("assemblyString");
74 Contract.EndContractBlock();
76 if ((assemblyString.Length == 0) ||
77 (assemblyString[0] == '\0'))
78 throw new ArgumentException(Environment.GetResourceString("Format_StringZeroLength"));
81 AppDomain.CheckReflectionOnlyLoadSupported();
83 AssemblyName an = new AssemblyName();
85 an.Name = assemblyString;
86 assemblyFromResolveEvent = null; // instead of an.nInit(out assemblyFromResolveEvent, forIntrospection, true);
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,
98 bool throwOnFileNotFound,
99 bool forIntrospection,
100 bool suppressSecurityChecks)
102 if (assemblyRef == null)
103 throw new ArgumentNullException("assemblyRef");
104 Contract.EndContractBlock();
106 if (assemblyRef.CodeBase != null)
108 AppDomain.CheckLoadFromSupported();
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;
120 if (assemblySecurity != null)
122 #if FEATURE_CAS_POLICY
123 if (!AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
125 throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
127 #endif // FEATURE_CAS_POLICY
129 if (!suppressSecurityChecks)
131 #pragma warning disable 618
132 new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Demand();
133 #pragma warning restore 618
137 return (RuntimeAssembly) Assembly.Load (assemblyRef);
140 internal static RuntimeAssembly LoadWithPartialNameInternal (String partialName, Evidence securityEvidence, ref StackCrawlMark stackMark)
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);
147 internal static RuntimeAssembly LoadWithPartialNameInternal (AssemblyName an, Evidence securityEvidence, ref StackCrawlMark stackMark)
149 return LoadWithPartialNameInternal (an.ToString (), securityEvidence, ref stackMark);
155 [ComDefaultInterfaceAttribute (typeof (_Assembly))]
157 [ClassInterface(ClassInterfaceType.None)]
158 class MonoAssembly : RuntimeAssembly
162 Type GetType (string name, bool throwOnError, bool ignoreCase)
166 throw new ArgumentNullException (name);
167 if (name.Length == 0)
168 throw new ArgumentException ("name", "Name cannot be empty");
170 res = InternalGetType (null, name, throwOnError, ignoreCase);
176 Module GetModule (String name)
179 throw new ArgumentNullException ("name");
180 if (name.Length == 0)
181 throw new ArgumentException ("Name can't be empty");
183 Module[] modules = GetModules (true);
184 foreach (Module module in modules) {
185 if (module.ScopeName == name)
194 AssemblyName[] GetReferencedAssemblies () {
195 return GetReferencedAssemblies (this);
200 Module[] GetModules (bool getResourceModules) {
201 Module[] modules = GetModulesInternal ();
203 if (!getResourceModules) {
204 var result = new List<Module> (modules.Length);
205 foreach (Module m in modules)
206 if (!m.IsResource ())
208 return result.ToArray ();
214 [MonoTODO ("Always returns the same as GetModules")]
217 Module[] GetLoadedModules (bool getResourceModules)
219 return GetModules (getResourceModules);
224 Assembly GetSatelliteAssembly (CultureInfo culture)
226 return GetSatelliteAssembly (culture, null, true);
231 Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
233 return GetSatelliteAssembly (culture, version, true);
236 //FIXME remove GetManifestModule under v4, it's a v2 artifact
240 Module ManifestModule {
242 return GetManifestModule ();
248 bool GlobalAssemblyCache {
250 return get_global_assembly_cache ();