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 AssemblyName an = new AssemblyName(partialName);
143 return LoadWithPartialNameInternal (an, securityEvidence, ref stackMark);
146 internal static RuntimeAssembly LoadWithPartialNameInternal (AssemblyName an, Evidence securityEvidence, ref StackCrawlMark stackMark)
148 throw new NotImplementedException ("LoadWithPartialNameInternal");
153 [ComDefaultInterfaceAttribute (typeof (_Assembly))]
155 [ClassInterface(ClassInterfaceType.None)]
156 class MonoAssembly : RuntimeAssembly
160 Type GetType (string name, bool throwOnError, bool ignoreCase)
164 throw new ArgumentNullException (name);
165 if (name.Length == 0)
166 throw new ArgumentException ("name", "Name cannot be empty");
168 res = InternalGetType (null, name, throwOnError, ignoreCase);
174 Module GetModule (String name)
177 throw new ArgumentNullException ("name");
178 if (name.Length == 0)
179 throw new ArgumentException ("Name can't be empty");
181 Module[] modules = GetModules (true);
182 foreach (Module module in modules) {
183 if (module.ScopeName == name)
192 AssemblyName[] GetReferencedAssemblies () {
193 return GetReferencedAssemblies (this);
198 Module[] GetModules (bool getResourceModules) {
199 Module[] modules = GetModulesInternal ();
201 if (!getResourceModules) {
202 var result = new List<Module> (modules.Length);
203 foreach (Module m in modules)
204 if (!m.IsResource ())
206 return result.ToArray ();
212 [MonoTODO ("Always returns the same as GetModules")]
215 Module[] GetLoadedModules (bool getResourceModules)
217 return GetModules (getResourceModules);
222 Assembly GetSatelliteAssembly (CultureInfo culture)
224 return GetSatelliteAssembly (culture, null, true);
229 Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
231 return GetSatelliteAssembly (culture, version, true);
234 //FIXME remove GetManifestModule under v4, it's a v2 artifact
238 Module ManifestModule {
240 return GetManifestModule ();
246 bool GlobalAssemblyCache {
248 return get_global_assembly_cache ();