Edit

Share via


Assembly.LoadFrom Method

Definition

Loads an assembly.

Overloads

LoadFrom(String, Byte[], AssemblyHashAlgorithm)
Obsolete.

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

LoadFrom(String, Evidence, Byte[], AssemblyHashAlgorithm)
Obsolete.

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

LoadFrom(String)

Loads an assembly given its file name or path.

LoadFrom(String, Evidence)
Obsolete.

Loads an assembly given its file name or path and supplying security evidence.

Remarks

.NET Framework only: See <loadFromRemoteSources> for loading assemblies from remote locations.

LoadFrom(String, Byte[], AssemblyHashAlgorithm)

Source:
Assembly.cs
Source:
Assembly.cs
Source:
Assembly.cs

Caution

LoadFrom with a custom AssemblyHashAlgorithm is obsolete. Use overloads without an AssemblyHashAlgorithm.

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

public:
 static System::Reflection::Assembly ^ LoadFrom(System::String ^ assemblyFile, cli::array <System::Byte> ^ hashValue, System::Configuration::Assemblies::AssemblyHashAlgorithm hashAlgorithm);
public static System.Reflection.Assembly LoadFrom (string assemblyFile, byte[]? hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
[System.Obsolete("LoadFrom with a custom AssemblyHashAlgorithm is obsolete. Use overloads without an AssemblyHashAlgorithm.", DiagnosticId="SYSLIB0056", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static System.Reflection.Assembly LoadFrom (string assemblyFile, byte[]? hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
public static System.Reflection.Assembly LoadFrom (string assemblyFile, byte[] hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
static member LoadFrom : string * byte[] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Reflection.Assembly
[<System.Obsolete("LoadFrom with a custom AssemblyHashAlgorithm is obsolete. Use overloads without an AssemblyHashAlgorithm.", DiagnosticId="SYSLIB0056", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
static member LoadFrom : string * byte[] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Reflection.Assembly
Public Shared Function LoadFrom (assemblyFile As String, hashValue As Byte(), hashAlgorithm As AssemblyHashAlgorithm) As Assembly

Parameters

assemblyFile
String

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

hashValue
Byte[]

The value of the computed hash code.

hashAlgorithm
AssemblyHashAlgorithm

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

Returns

The loaded assembly.

Attributes

Exceptions

.NET Core and .NET 5+ only: In all cases.

assemblyFile is null.

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

A file that was found could not be loaded.

-or-

assemblyFile specifies a location that is disabled based on <loadFromRemoteSources>.

assemblyFile is not a valid assembly for the currently loaded runtime; for example, a 32-bit assembly in a 64-bit process.

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

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

The assembly name exceeds the system-defined maximum length.

Remarks

In .NET Core and .NET 5+, this method throws NotSupportedException when called. Use LoadFrom(String) instead.

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

Note

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 can be absolute or relative to the current directory.

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 global assembly cache, 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 that is not included in probing. LoadFrom, CreateInstanceFrom, and ExecuteAssembly are examples of methods that load by path.

    See <loadFromRemoteSources> for loading assemblies from remote locations.

  • 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 that is 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 the same 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.

The assembly is loaded with the evidence that the loader supplies.

Applies to

LoadFrom(String, Evidence, Byte[], AssemblyHashAlgorithm)

Caution

This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of LoadFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

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

public:
 static System::Reflection::Assembly ^ LoadFrom(System::String ^ assemblyFile, System::Security::Policy::Evidence ^ securityEvidence, cli::array <System::Byte> ^ hashValue, System::Configuration::Assemblies::AssemblyHashAlgorithm hashAlgorithm);
public static System.Reflection.Assembly LoadFrom (string assemblyFile, System.Security.Policy.Evidence securityEvidence, byte[] hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
[System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of LoadFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public static System.Reflection.Assembly LoadFrom (string assemblyFile, System.Security.Policy.Evidence securityEvidence, byte[] hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
static member LoadFrom : string * System.Security.Policy.Evidence * byte[] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Reflection.Assembly
[<System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of LoadFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
static member LoadFrom : string * System.Security.Policy.Evidence * byte[] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Reflection.Assembly
Public Shared Function LoadFrom (assemblyFile As String, securityEvidence As Evidence, hashValue As Byte(), hashAlgorithm As AssemblyHashAlgorithm) As Assembly

Parameters

assemblyFile
String

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

securityEvidence
Evidence

Evidence for loading the assembly.

hashValue
Byte[]

The value of the computed hash code.

hashAlgorithm
AssemblyHashAlgorithm

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

Returns

The loaded assembly.

Attributes

Exceptions

assemblyFile is null.

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

A file that was found could not be loaded.

-or-

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

-or-

assemblyFile specifies a location that is disabled based on <loadFromRemoteSources>.

assemblyFile is not a valid assembly for the currently loaded runtime; for example, a 32-bit assembly in a 64-bit process.

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

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

The assembly name exceeds the system-defined maximum length.

Remarks

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

Note

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 can be absolute or relative to the current directory.

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.

    See <loadFromRemoteSources> for loading assemblies from remote locations.

  • 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 the same 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.

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.

Applies to

LoadFrom(String)

Source:
Assembly.cs
Source:
Assembly.cs
Source:
Assembly.cs

Loads an assembly given its file name or path.

public:
 static System::Reflection::Assembly ^ LoadFrom(System::String ^ assemblyFile);
public static System.Reflection.Assembly LoadFrom (string assemblyFile);
static member LoadFrom : string -> System.Reflection.Assembly
Public Shared Function LoadFrom (assemblyFile As String) As Assembly

Parameters

assemblyFile
String

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

Returns

The loaded assembly.

Exceptions

assemblyFile is null.

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

A file that was found could not be loaded.

-or-

.NET Framework only: assemblyFile specifies a location that is disabled based on <loadFromRemoteSources>.

assemblyFile is not a valid assembly for the currently loaded runtime; for example, a 32-bit assembly in a 64-bit process.

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

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

The assembly name exceeds the system-defined maximum length.

Examples

The following example loads an assembly given its file name or path.

Assembly^ SampleAssembly;
SampleAssembly = Assembly::LoadFrom( "c:\\Sample.Assembly.dll" );
// Obtain a reference to a method known to exist in assembly.
MethodInfo^ Method = SampleAssembly->GetTypes()[ 0 ]->GetMethod( "Method1" );
// Obtain a reference to the parameters collection of the MethodInfo instance.
array<ParameterInfo^>^ Params = Method->GetParameters();
// Display information about method parameters.
// Param = sParam1
//   Type = System::String
//   Position = 0
//   Optional=False
for each ( ParameterInfo^ Param in Params )
{
   Console::WriteLine( "Param= {0}", Param->Name );
   Console::WriteLine( "  Type= {0}", Param->ParameterType );
   Console::WriteLine( "  Position= {0}", Param->Position );
   Console::WriteLine( "  Optional= {0}", Param->IsOptional );
}
Assembly SampleAssembly;
SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
// Obtain a reference to a method known to exist in assembly.
MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");
// Obtain a reference to the parameters collection of the MethodInfo instance.
ParameterInfo[] Params = Method.GetParameters();
// Display information about method parameters.
// Param = sParam1
//   Type = System.String
//   Position = 0
//   Optional=False
foreach (ParameterInfo Param in Params)
{
    Console.WriteLine("Param=" + Param.Name.ToString());
    Console.WriteLine("  Type=" + Param.ParameterType.ToString());
    Console.WriteLine("  Position=" + Param.Position.ToString());
    Console.WriteLine("  Optional=" + Param.IsOptional.ToString());
}
Dim SampleAssembly As [Assembly]
SampleAssembly = [Assembly].LoadFrom("c:\Sample.Assembly.dll")
' Obtain a reference to a method known to exist in assembly.
Dim Method As MethodInfo = SampleAssembly.GetTypes()(0).GetMethod("Method1")
' Obtain a reference to the parameters collection of the MethodInfo instance.
Dim Params As ParameterInfo() = Method.GetParameters()
' Display information about method parameters.
' Param = sParam1
'   Type = System.String
'   Position = 0
'   Optional=False
For Each Param As ParameterInfo In Params
    Console.WriteLine(("Param=" + Param.Name.ToString()))
    Console.WriteLine(("  Type=" + Param.ParameterType.ToString()))
    Console.WriteLine(("  Position=" + Param.Position.ToString()))
    Console.WriteLine(("  Optional=" + Param.IsOptional.ToString()))
Next

Remarks

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

Note

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 may be absolute or relative to the current directory.

.NET Framework only: 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. It also allows dependencies on that path to be found and loaded because the path information is maintained by the context. LoadFrom, CreateInstanceFrom, and ExecuteAssembly are examples of methods that load by path.

    See <loadFromRemoteSources> for loading assemblies from remote locations.

  • 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 LoadFrom method has the following disadvantages. Consider using Load instead.

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

  • An assembly can be loaded in the load-from context even though an assembly with the same identity exists in the load context. Interoperability between the two assemblies will not work, leading to errors such as InvalidCastException, MissingMethodException, or other unexpected behavior.

  • Calling LoadFrom with a location that's in the probing path will load the assembly in the load context and not in the load-from context.

  • If an assembly file whose identity is goverened by a binding redirect policy is passed to LoadFrom, the policy will be applied and the assembly will be loaded from the probing path in the load context.

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

  • 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.

Applies to

LoadFrom(String, Evidence)

Caution

This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of LoadFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

Loads an assembly given its file name or path and supplying security evidence.

public:
 static System::Reflection::Assembly ^ LoadFrom(System::String ^ assemblyFile, System::Security::Policy::Evidence ^ securityEvidence);
public static System.Reflection.Assembly LoadFrom (string assemblyFile, System.Security.Policy.Evidence securityEvidence);
[System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of LoadFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public static System.Reflection.Assembly LoadFrom (string assemblyFile, System.Security.Policy.Evidence securityEvidence);
static member LoadFrom : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
[<System.Obsolete("This method is obsolete and will be removed in a future release of the .NET Framework. Please use an overload of LoadFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
static member LoadFrom : string * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Shared Function LoadFrom (assemblyFile As String, securityEvidence As Evidence) As Assembly

Parameters

assemblyFile
String

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

securityEvidence
Evidence

Evidence for loading the assembly.

Returns

The loaded assembly.

Attributes

Exceptions

assemblyFile is null.

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

A file that was found could not be loaded.

-or-

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

-or-

assemblyFile specifies a location that is disabled based on <loadFromRemoteSources>.

assemblyFile is not a valid assembly for the currently loaded runtime; for example, a 32-bit assembly in a 64-bit process.

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

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

The assembly name exceeds the system-defined maximum length.

Remarks

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

Note

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 can be absolute or relative to the current directory.

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.

    See <loadFromRemoteSources> for loading assemblies from remote locations.

  • 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 the same 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.

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.

See also

Applies to