2 // System.Reflection/Assembly.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System
.Security
;
32 using System
.Security
.Policy
;
33 using System
.Security
.Permissions
;
34 using System
.Runtime
.Serialization
;
35 using System
.Reflection
;
37 using System
.Globalization
;
38 using System
.Runtime
.CompilerServices
;
39 using System
.Runtime
.InteropServices
;
40 using System
.Collections
.Generic
;
41 using System
.Configuration
.Assemblies
;
42 using System
.Threading
;
44 using System
.Diagnostics
.Contracts
;
48 namespace System
.Reflection
{
51 [ComDefaultInterfaceAttribute (typeof (_Assembly
))]
53 [ClassInterface(ClassInterfaceType
.None
)]
54 [StructLayout (LayoutKind
.Sequential
)]
56 public partial class Assembly
: ICustomAttributeProvider
, ISerializable
58 public abstract class Assembly
: ICustomAttributeProvider
, _Assembly
, IEvidenceFactory
, ISerializable
61 internal class ResolveEventHolder
{
62 #pragma warning disable 67
63 public event ModuleResolveEventHandler ModuleResolve
;
64 #pragma warning restore
68 // We can't store the event directly in this class, since the
69 // compiler would silently insert the fields before _mono_assembly
71 public virtual event ModuleResolveEventHandler ModuleResolve
{
72 [SecurityPermission (SecurityAction
.LinkDemand
, ControlAppDomain
= true)]
74 throw new NotImplementedException ();
76 [SecurityPermission (SecurityAction
.LinkDemand
, ControlAppDomain
= true)]
78 throw new NotImplementedException ();
82 public virtual string CodeBase
{
84 throw new NotImplementedException ();
88 public virtual string EscapedCodeBase
{
90 throw new NotImplementedException ();
94 public virtual string FullName
{
96 throw new NotImplementedException ();
100 public virtual MethodInfo EntryPoint
{
102 throw new NotImplementedException ();
106 public virtual Evidence Evidence
{
107 [SecurityPermission (SecurityAction
.Demand
, ControlEvidence
= true)]
109 throw new NotImplementedException ();
113 internal virtual Evidence
UnprotectedGetEvidence ()
115 throw new NotImplementedException ();
118 internal virtual IntPtr MonoAssembly
{
120 throw new NotImplementedException ();
124 internal virtual bool FromByteArray
{
126 throw new NotImplementedException ();
130 public virtual String Location
{
132 throw new NotImplementedException ();
137 public virtual string ImageRuntimeVersion
{
139 throw new NotImplementedException ();
143 public virtual void GetObjectData (SerializationInfo info
, StreamingContext context
)
145 throw new NotImplementedException ();
148 public virtual bool IsDefined (Type attributeType
, bool inherit
)
150 throw new NotImplementedException ();
153 public virtual object [] GetCustomAttributes (bool inherit
)
155 throw new NotImplementedException ();
158 public virtual object [] GetCustomAttributes (Type attributeType
, bool inherit
)
160 throw new NotImplementedException ();
163 public virtual FileStream
[] GetFiles ()
165 return GetFiles (false);
168 public virtual FileStream
[] GetFiles (bool getResourceModules
)
170 throw new NotImplementedException ();
173 public virtual FileStream
GetFile (String name
)
175 throw new NotImplementedException ();
178 public virtual Stream
GetManifestResourceStream (String name
)
180 throw new NotImplementedException ();
183 public virtual Stream
GetManifestResourceStream (Type type
, String name
)
185 throw new NotImplementedException ();
188 internal Stream
GetManifestResourceStream (Type type
, String name
, bool skipSecurityCheck
, ref StackCrawlMark stackMark
)
190 StringBuilder sb
= new StringBuilder ();
193 throw new ArgumentNullException ("type");
195 String nameSpace
= type
.Namespace
;
196 if (nameSpace
!= null) {
197 sb
.Append (nameSpace
);
199 sb
.Append (Type
.Delimiter
);
206 return GetManifestResourceStream (sb
.ToString());
209 internal unsafe Stream
GetManifestResourceStream(String name
, ref StackCrawlMark stackMark
, bool skipSecurityCheck
)
211 return GetManifestResourceStream (null, name
, skipSecurityCheck
, ref stackMark
);
214 internal String
GetSimpleName()
216 AssemblyName aname
= GetName (true);
220 internal byte[] GetPublicKey()
222 AssemblyName aname
= GetName (true);
223 return aname
.GetPublicKey ();
226 internal Version
GetVersion()
228 AssemblyName aname
= GetName (true);
229 return aname
.Version
;
232 private AssemblyNameFlags
GetFlags()
234 AssemblyName aname
= GetName (true);
238 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
239 internal virtual extern Type
[] GetTypes (bool exportedOnly
);
241 public virtual Type
[] GetTypes ()
243 return GetTypes (false);
246 public virtual Type
[] GetExportedTypes ()
248 throw new NotImplementedException ();
251 public virtual Type
GetType (String name
, Boolean throwOnError
)
253 return GetType (name
, throwOnError
, false);
256 public virtual Type
GetType (String name
) {
257 return GetType (name
, false, false);
260 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
261 internal extern Type
InternalGetType (Module module
, String name
, Boolean throwOnError
, Boolean ignoreCase
);
263 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
264 internal extern unsafe static void InternalGetAssemblyName (string assemblyFile
, out Mono
.MonoAssemblyName aname
, out string codebase
);
266 public virtual AssemblyName
GetName (Boolean copiedName
)
268 throw new NotImplementedException ();
271 public virtual AssemblyName
GetName ()
273 return GetName (false);
276 public override string ToString ()
278 return base.ToString ();
281 public static String
CreateQualifiedName (String assemblyName
, String typeName
)
283 return typeName
+ ", " + assemblyName
;
286 public static Assembly
GetAssembly (Type type
)
289 return type
.Assembly
;
290 throw new ArgumentNullException ("type");
294 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
295 public static extern Assembly
GetEntryAssembly();
297 internal Assembly
GetSatelliteAssembly (CultureInfo culture
, Version version
, bool throwOnError
, ref StackCrawlMark stackMark
)
300 throw new ArgumentNullException("culture");
301 Contract
.EndContractBlock();
303 String name
= GetSimpleName() + ".resources";
304 return InternalGetSatelliteAssembly(name
, culture
, version
, true, ref stackMark
);
307 internal RuntimeAssembly
InternalGetSatelliteAssembly (String name
, CultureInfo culture
, Version version
, bool throwOnFileNotFound
, ref StackCrawlMark stackMark
)
309 AssemblyName an
= new AssemblyName ();
311 an
.SetPublicKey (GetPublicKey ());
312 an
.Flags
= GetFlags () | AssemblyNameFlags
.PublicKey
;
315 an
.Version
= GetVersion ();
317 an
.Version
= version
;
319 an
.CultureInfo
= culture
;
325 assembly
= AppDomain
.CurrentDomain
.LoadSatellite (an
, false, ref stackMark
);
326 if (assembly
!= null)
327 return (RuntimeAssembly
)assembly
;
328 } catch (FileNotFoundException
) {
333 if (String
.IsNullOrEmpty (Location
))
336 // Try the assembly directory
337 string location
= Path
.GetDirectoryName (Location
);
338 string fullName
= Path
.Combine (location
, Path
.Combine (culture
.Name
, an
.Name
+ ".dll"));
341 return (RuntimeAssembly
)LoadFrom (fullName
, false, ref stackMark
);
343 if (!throwOnFileNotFound
&& !File
.Exists (fullName
))
350 Type _Assembly
.GetType ()
352 // Required or object::GetType becomes virtual final
353 return base.GetType ();
357 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
358 private extern static Assembly
LoadFrom (String assemblyFile
, bool refOnly
, ref StackCrawlMark stackMark
);
360 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
361 private extern static Assembly
LoadFile_internal (String assemblyFile
, ref StackCrawlMark stackMark
);
363 [MethodImplAttribute(MethodImplOptions
.NoInlining
)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
364 public static Assembly
LoadFrom (String assemblyFile
)
366 StackCrawlMark stackMark
= StackCrawlMark
.LookForMyCaller
;
367 return LoadFrom (assemblyFile
, false, ref stackMark
);
371 [MethodImplAttribute(MethodImplOptions
.NoInlining
)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
372 public static Assembly
LoadFrom (String assemblyFile
, Evidence securityEvidence
)
374 StackCrawlMark stackMark
= StackCrawlMark
.LookForMyCaller
;
375 Assembly a
= LoadFrom (assemblyFile
, false, ref stackMark
);
377 if ((a
!= null) && (securityEvidence
!= null)) {
378 // merge evidence (i.e. replace defaults with provided evidences)
379 a
.Evidence
.Merge (securityEvidence
);
386 [MonoTODO("This overload is not currently implemented")]
387 // FIXME: What are we missing?
388 public static Assembly
LoadFrom (String assemblyFile
, Evidence securityEvidence
, byte[] hashValue
, AssemblyHashAlgorithm hashAlgorithm
)
390 throw new NotImplementedException ();
394 public static Assembly
LoadFrom (String assemblyFile
, byte [] hashValue
, AssemblyHashAlgorithm hashAlgorithm
)
396 throw new NotImplementedException ();
399 [MethodImplAttribute(MethodImplOptions
.NoInlining
)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
400 public static Assembly
UnsafeLoadFrom (String assemblyFile
)
402 StackCrawlMark stackMark
= StackCrawlMark
.LookForMyCaller
;
403 return LoadFrom (assemblyFile
, false, ref stackMark
);
407 [MethodImplAttribute(MethodImplOptions
.NoInlining
)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
408 public static Assembly
LoadFile (String path
, Evidence securityEvidence
)
411 throw new ArgumentNullException ("path");
412 if (path
== String
.Empty
)
413 throw new ArgumentException ("Path can't be empty", "path");
414 StackCrawlMark stackMark
= StackCrawlMark
.LookForMyCaller
;
415 Assembly a
= LoadFile_internal (path
, ref stackMark
);
416 if (a
!= null && securityEvidence
!= null) {
417 throw new NotImplementedException ();
422 public static Assembly
LoadFile (String path
)
424 return LoadFile (path
, null);
427 public static Assembly
Load (String assemblyString
)
429 return AppDomain
.CurrentDomain
.Load (assemblyString
);
433 public static Assembly
Load (String assemblyString
, Evidence assemblySecurity
)
435 return AppDomain
.CurrentDomain
.Load (assemblyString
, assemblySecurity
);
438 public static Assembly
Load (AssemblyName assemblyRef
)
440 return AppDomain
.CurrentDomain
.Load (assemblyRef
);
444 public static Assembly
Load (AssemblyName assemblyRef
, Evidence assemblySecurity
)
446 return AppDomain
.CurrentDomain
.Load (assemblyRef
, assemblySecurity
);
449 public static Assembly
Load (Byte
[] rawAssembly
)
451 return AppDomain
.CurrentDomain
.Load (rawAssembly
);
454 public static Assembly
Load (Byte
[] rawAssembly
, Byte
[] rawSymbolStore
)
456 return AppDomain
.CurrentDomain
.Load (rawAssembly
, rawSymbolStore
);
460 public static Assembly
Load (Byte
[] rawAssembly
, Byte
[] rawSymbolStore
,
461 Evidence securityEvidence
)
463 return AppDomain
.CurrentDomain
.Load (rawAssembly
, rawSymbolStore
, securityEvidence
);
466 [MonoLimitation ("Argument securityContextSource is ignored")]
467 public static Assembly
Load (byte [] rawAssembly
, byte [] rawSymbolStore
, SecurityContextSource securityContextSource
)
469 return AppDomain
.CurrentDomain
.Load (rawAssembly
, rawSymbolStore
);
472 public static Assembly
ReflectionOnlyLoad (byte[] rawAssembly
)
474 return AppDomain
.CurrentDomain
.Load (rawAssembly
, null, null, true);
477 [MethodImplAttribute(MethodImplOptions
.NoInlining
)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
478 public static Assembly
ReflectionOnlyLoad (string assemblyString
)
480 StackCrawlMark stackMark
= StackCrawlMark
.LookForMyCaller
;
481 return AppDomain
.CurrentDomain
.Load (assemblyString
, null, true, ref stackMark
);
484 [MethodImplAttribute(MethodImplOptions
.NoInlining
)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
485 public static Assembly
ReflectionOnlyLoadFrom (string assemblyFile
)
487 if (assemblyFile
== null)
488 throw new ArgumentNullException ("assemblyFile");
490 StackCrawlMark stackMark
= StackCrawlMark
.LookForMyCaller
;
491 return LoadFrom (assemblyFile
, true, ref stackMark
);
494 [Obsolete("This method has been deprecated. Please use Assembly.Load() instead. http://go.microsoft.com/fwlink/?linkid=14202")]
495 public static Assembly
LoadWithPartialName (string partialName
)
497 return LoadWithPartialName (partialName
, null);
500 [MonoTODO ("Not implemented")]
501 public Module
LoadModule (string moduleName
, byte [] rawModule
)
503 throw new NotImplementedException ();
506 [MonoTODO ("Not implemented")]
509 Module
LoadModule (string moduleName
, byte [] rawModule
, byte [] rawSymbolStore
)
511 throw new NotImplementedException ();
514 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
515 private static extern Assembly
load_with_partial_name (string name
, Evidence e
);
517 [Obsolete("This method has been deprecated. Please use Assembly.Load() instead. http://go.microsoft.com/fwlink/?linkid=14202")]
518 public static Assembly
LoadWithPartialName (string partialName
, Evidence securityEvidence
)
520 return LoadWithPartialName (partialName
, securityEvidence
, true);
524 * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
525 * is a valid gac name and contains filesystem entry charachters at the end of the name
526 * ie System/// will throw an exception. However ////System will not as that is canocolized
530 // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
531 internal static Assembly
LoadWithPartialName (string partialName
, Evidence securityEvidence
, bool oldBehavior
)
534 throw new NotImplementedException ();
536 if (partialName
== null)
537 throw new NullReferenceException ();
539 return load_with_partial_name (partialName
, securityEvidence
);
542 public Object
CreateInstance (String typeName
)
544 return CreateInstance (typeName
, false);
547 public Object
CreateInstance (String typeName
, Boolean ignoreCase
)
549 Type t
= GetType (typeName
, false, ignoreCase
);
554 return Activator
.CreateInstance (t
);
555 } catch (InvalidOperationException
) {
556 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
562 Object
CreateInstance (String typeName
, Boolean ignoreCase
,
563 BindingFlags bindingAttr
, Binder binder
,
564 Object
[] args
, CultureInfo culture
,
565 Object
[] activationAttributes
)
567 Type t
= GetType (typeName
, false, ignoreCase
);
572 return Activator
.CreateInstance (t
, bindingAttr
, binder
, args
, culture
, activationAttributes
);
573 } catch (InvalidOperationException
) {
574 throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
578 public Module
[] GetLoadedModules ()
580 return GetLoadedModules (false);
583 public Module
[] GetModules ()
585 return GetModules (false);
588 internal virtual Module
[] GetModulesInternal () {
589 throw new NotImplementedException ();
592 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
593 public extern static Assembly
GetExecutingAssembly ();
595 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
596 public extern static Assembly
GetCallingAssembly ();
598 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
599 internal static extern IntPtr
InternalGetReferencedAssemblies (Assembly module
);
601 public virtual String
[] GetManifestResourceNames ()
603 throw new NotImplementedException ();
606 internal static AssemblyName
[] GetReferencedAssemblies (Assembly module
)
608 using (var nativeNames
= new Mono
.SafeGPtrArrayHandle (InternalGetReferencedAssemblies (module
))) {
609 var numAssemblies
= nativeNames
.Length
;
611 AssemblyName
[] result
= new AssemblyName
[numAssemblies
];
612 const bool addVersion
= true;
613 const bool addPublicKey
= false;
614 const bool defaultToken
= true;
615 const bool assemblyRef
= true;
616 for (int i
= 0; i
< numAssemblies
; i
++) {
617 AssemblyName name
= new AssemblyName ();
619 Mono
.MonoAssemblyName
*nativeName
= (Mono
.MonoAssemblyName
*) nativeNames
[i
];
620 name
.FillName (nativeName
, null, addVersion
, addPublicKey
, defaultToken
, assemblyRef
);
626 for (int i
= 0; i
< numAssemblies
; i
++) {
628 Mono
.MonoAssemblyName
* nativeName
= (Mono
.MonoAssemblyName
*) nativeNames
[i
];
629 Mono
.RuntimeMarshal
.FreeAssemblyName (ref *nativeName
, true);
636 public virtual ManifestResourceInfo
GetManifestResourceInfo (String resourceName
)
638 throw new NotImplementedException ();
641 [MonoTODO ("Currently it always returns zero")]
649 internal virtual Module
GetManifestModule () {
650 throw new NotImplementedException ();
654 public virtual bool ReflectionOnly
{
656 throw new NotImplementedException ();
660 public override int GetHashCode ()
662 return base.GetHashCode ();
665 public override bool Equals (object o
)
667 return base.Equals (o
);
671 internal virtual PermissionSet GrantedPermissionSet
{
673 throw new NotImplementedException ();
677 internal virtual PermissionSet DeniedPermissionSet
{
679 throw new NotImplementedException ();
683 public virtual PermissionSet PermissionSet
{
685 throw new NotImplementedException ();
690 public virtual SecurityRuleSet SecurityRuleSet
{
691 get { throw CreateNIE (); }
694 static Exception
CreateNIE ()
696 return new NotImplementedException ("Derived classes must implement it");
699 public virtual IList
<CustomAttributeData
> GetCustomAttributesData ()
701 throw new NotImplementedException ();
705 public bool IsFullyTrusted
{
709 public virtual Type
GetType (string name
, bool throwOnError
, bool ignoreCase
)
714 public virtual Module
GetModule (String name
)
719 public virtual AssemblyName
[] GetReferencedAssemblies ()
724 public virtual Module
[] GetModules (bool getResourceModules
)
729 [MonoTODO ("Always returns the same as GetModules")]
730 public virtual Module
[] GetLoadedModules (bool getResourceModules
)
735 public virtual Assembly
GetSatelliteAssembly (CultureInfo culture
)
740 public virtual Assembly
GetSatelliteAssembly (CultureInfo culture
, Version version
)
745 public virtual Module ManifestModule
{
746 get { throw CreateNIE (); }
749 public virtual bool GlobalAssemblyCache
{
750 get { throw CreateNIE (); }
753 public virtual bool IsDynamic
{
754 get { return false; }
757 public static bool operator == (Assembly left
, Assembly right
)
759 if ((object)left
== (object)right
)
761 if ((object)left
== null ^
(object)right
== null)
763 return left
.Equals (right
);
766 public static bool operator != (Assembly left
, Assembly right
)
768 if ((object)left
== (object)right
)
770 if ((object)left
== null ^
(object)right
== null)
772 return !left
.Equals (right
);
775 public virtual IEnumerable
<TypeInfo
> DefinedTypes
{
777 foreach (var type
in GetTypes ()) {
778 yield return type
.GetTypeInfo ();
783 public virtual IEnumerable
<Type
> ExportedTypes
{
784 get { return GetExportedTypes (); }
787 public virtual IEnumerable
<Module
> Modules
{
788 get { return GetModules (); }
791 public virtual IEnumerable
<CustomAttributeData
> CustomAttributes
{
792 get { return GetCustomAttributesData (); }
795 public virtual Type
[] GetForwardedTypes() => throw new PlatformNotSupportedException();