Export (0) Print
Expand All

Assembly.LoadFrom Method (String, Evidence, Byte[], AssemblyHashAlgorithm)

Loads an assembly given its file name or path, security evidence hash value, and hash algorithm.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

public static Assembly LoadFrom(
	string assemblyFile,
	Evidence securityEvidence,
	byte[] hashValue,
	AssemblyHashAlgorithm hashAlgorithm
)

Parameters

assemblyFile
Type: System.String

The name or path of the file that contains the manifest of the assembly.

securityEvidence
Type: System.Security.Policy.Evidence

Evidence for loading the assembly.

hashValue
Type: System.Byte[]

The value of the computed hash code.

hashAlgorithm
Type: System.Configuration.Assemblies.AssemblyHashAlgorithm

The hash algorithm used for hashing files and for generating the strong name.

Return Value

Type: System.Reflection.Assembly
The loaded assembly.

ExceptionCondition
ArgumentNullException

assemblyFile is null.

FileNotFoundException

assemblyFile is not found, or the module you are trying to load does not specify a filename extension.

FileLoadException

A file that was found could not be loaded.

-or-

The securityEvidence is not ambiguous and is determined to be invalid.

BadImageFormatException

assemblyFile is not a valid assembly.

-or-

Version 2.0 or later of the common language runtime is currently loaded and assemblyFile was compiled with a later version.

SecurityException

A codebase that does not start with "file://" was specified without the required WebPermission.

ArgumentException

The assemblyFile parameter is an empty string ("").

PathTooLongException

The assembly name is longer than MAX_PATH characters.

The assemblyFile parameter must refer to a URI without escape characters. This method supplies escape characters for all invalid characters in the URI.

NoteNote:

File transfer protocol (FTP) is not supported. If the URI supplied for assemblyFile is an FTP address, the assembly is not loaded. No exception is thrown.

assemblyFile is relative to the current directory, and the assembly is loaded into the domain of the caller.

Assemblies can be loaded into one of three contexts, or can be loaded without context:

  • The load context contains assemblies found by probing: in the GAC, in a host assembly store if the runtime is hosted, or in the ApplicationBase and PrivateBinPath of the application domain. Most overloads of the Load method load assemblies into this context.

  • The load-from context contains assemblies for which the user provided a path not included in the directories searched by probing. LoadFrom, CreateInstanceFrom, and ExecuteAssembly are examples of methods that load by path.

  • The reflection-only context contains assemblies loaded with the ReflectionOnlyLoad and ReflectionOnlyLoadFrom methods; code in these contexts cannot be executed.

  • If the user generated or found the assembly, it is not in any context. This applies to assemblies loaded using overloads of the Load method that specify a byte array containing an assembly, and to transient dynamic assemblies created with reflection emit and not saved to disk.

The load-from context allows an assembly to be loaded from a path not included in probing, and yet allows dependencies on that path to be found and loaded because the path information is maintained by the context.

The LoadFrom method has the following disadvantages. Consider using Load instead.

  • If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.

  • If an assembly is loaded with LoadFrom, and later an assembly in the load context attempts to load same the assembly by display name, the load attempt fails. This can occur when an assembly is deserialized.

  • If an assembly is loaded with LoadFrom, and the probing path includes an assembly with the same identity but a different location, an InvalidCastException, MissingMethodException, or other unexpected behavior can occur.

  • LoadFrom demands FileIOPermissionAccess.Read and FileIOPermissionAccess.PathDiscovery, or WebPermission, on the specified path.

  • If a native image exists for assemblyFile, it is not used. The assembly cannot be loaded as domain neutral.

  • In the .NET Framework version 1.0 and 1.1, policy is not applied.

Whether certain permissions are granted or not granted to an assembly is based on evidence. The rules for assembly and security evidence merging are as follows:

  • When you use a LoadFrom method with no Evidence parameter, the assembly is loaded with the evidence that the loader supplies.

  • When you use a LoadFrom method with an Evidence parameter, pieces of evidence are merged. Pieces of evidence supplied as an argument to the LoadFrom method supersede pieces of evidence supplied by the loader.

  • If you call this method more than once on the same assembly but with a different evidence specified, the common language runtime does not throw a FileLoadException because the equality and integrity of the different evidence specifications cannot be determined. The evidence that first succeeds is the evidence that is used.

  • When you use a LoadFrom method with a Byte[] parameter to load a common object file format (COFF) image, evidence is combined. Zone, Url and Site are inherited from the calling assembly, and Hash and StrongName are taken from the COFF assembly.

  • When you use a LoadFrom method with a Byte[] parameter and Evidence to load a COFF image, only the supplied evidence is used. Evidence of the calling assembly and evidence of the COFF image is ignored.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1

Community Additions

ADD
Show:
© 2014 Microsoft