通过


Assembly.LoadFrom 方法

定义

加载程序集。

重载

名称 说明
LoadFrom(String, Byte[], AssemblyHashAlgorithm)
已过时.

根据程序集的文件名或路径、哈希值和哈希算法加载程序集。

LoadFrom(String, Evidence, Byte[], AssemblyHashAlgorithm)
已过时.

根据程序集的文件名或路径、安全证据、哈希值和哈希算法加载程序集。

LoadFrom(String)

加载给定其文件名或路径的程序集。

LoadFrom(String, Evidence)
已过时.

加载给定程序集的文件名或路径并提供安全证据。

注解

仅限 .NET Framework: 请参阅 <loadFromRemoteSources> 从远程位置加载程序集。

LoadFrom(String, Byte[], AssemblyHashAlgorithm)

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

注意

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

根据程序集的文件名或路径、哈希值和哈希算法加载程序集。

public:
 static System::Reflection::Assembly ^ LoadFrom(System::String ^ assemblyFile, cli::array <System::Byte> ^ hashValue, System::Configuration::Assemblies::AssemblyHashAlgorithm hashAlgorithm);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
[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);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
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);
public static System.Reflection.Assembly LoadFrom(string assemblyFile, byte[]? hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")>]
[<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
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")>]
static member LoadFrom : string * byte[] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Reflection.Assembly
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

参数

assemblyFile
String

包含程序集清单的文件的名称或路径。

hashValue
Byte[]

计算哈希代码的值。

hashAlgorithm
AssemblyHashAlgorithm

用于哈希文件和生成强名称的哈希算法。

返回

加载的程序集。

属性

例外

仅限 .NET Core 和 .NET 5+ :在所有情况下。

assemblyFilenull

assemblyFile 找不到或尝试加载的模块未指定文件扩展名。

找不到的文件无法加载。

-或-

assemblyFile 指定基于 <loadFromRemoteSources> 禁用的位置。

assemblyFile 不是当前加载的运行时的有效程序集;例如,64 位进程中的 32 位程序集。

如果未指定以“file://”开头的代码库,则不需要 WebPermission

参数 assemblyFile 是空字符串(“)。

程序集名称超过系统定义的最大长度。

注解

.NET Core/.NET 5+ 中不支持此 API。 此方法在 NotSupportedException 调用时引发。 请改用 LoadFrom(String)

参数 assemblyFile 必须引用不带转义字符的 URI。 此方法为 URI 中的所有无效字符提供转义字符。

注释

不支持文件传输协议(FTP)。 如果提供的 assemblyFile URI 是 FTP 地址,则不会加载程序集。 不会引发异常。

assemblyFile 可以是绝对目录或相对于当前目录。

程序集可以加载到三个上下文中的一个,也可以在没有上下文的情况下加载程序集:

  • 加载上下文包含通过探测找到的程序集:在全局程序集缓存、托管运行时或应用程序域中的主机程序集存储中ApplicationBasePrivateBinPathLoad 方法的大多数重载都将程序集加载到此上下文中。

  • 从上下文加载的程序集包含用户为其提供了未包含在探测中的路径的程序集。 LoadFromCreateInstanceFrom以及 ExecuteAssembly 按路径加载的方法示例。

    请参阅 <loadFromRemoteSources> 从远程位置加载程序集。

  • 仅反射上下文包含使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法加载的程序集;无法执行这些上下文中的代码。

  • 如果用户生成或找到了程序集,则它没有任何上下文。 这适用于使用方法重载加载的 Load 程序集,该方法的重载指定包含程序集的字节数组,以及使用反射发出创建的暂时性动态程序集,而不是保存到磁盘。

从上下文加载允许从未包含在探测中的路径加载程序集,但允许找到并加载该路径的依赖项,因为路径信息由上下文维护。

该方法 LoadFrom 具有以下缺点。 请考虑改用 Load

  • 如果已加载具有相同标识的程序集,即使指定了其他路径, LoadFrom 也会返回已加载的程序集。

  • 如果加载 LoadFrom了程序集,并且以后加载上下文中的程序集会尝试通过显示名称加载同一程序集,则加载尝试将失败。 对程序集进行反序列化时,可能发生这种情况。

  • 如果程序集已LoadFrom加载,并且探测路径包含具有相同标识的程序集,但可能会发生其他位置、一个InvalidCastExceptionMissingMethodException或其他意外行为。

  • LoadFrom 需要对指定路径的 FileIOPermissionAccess.ReadFileIOPermissionAccess.PathDiscoveryWebPermission

  • 如果存在 assemblyFile本机映像,则不使用它。 程序集无法以域中立方式加载。

该程序集加载有加载程序提供的证据。

适用于

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

注意

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(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

参数

assemblyFile
String

包含程序集清单的文件的名称或路径。

securityEvidence
Evidence

加载程序集的证据。

hashValue
Byte[]

计算哈希代码的值。

hashAlgorithm
AssemblyHashAlgorithm

用于哈希文件和生成强名称的哈希算法。

返回

加载的程序集。

属性

例外

assemblyFilenull

assemblyFile 找不到或尝试加载的模块未指定文件扩展名。

找不到的文件无法加载。

-或-

securityEvidence 不明确,确定为无效。

-或-

assemblyFile 指定基于 <loadFromRemoteSources> 禁用的位置。

assemblyFile 不是当前加载的运行时的有效程序集;例如,64 位进程中的 32 位程序集。

如果未指定以“file://”开头的代码库,则不需要 WebPermission

参数 assemblyFile 是空字符串(“)。

程序集名称超过系统定义的最大长度。

注解

参数 assemblyFile 必须引用不带转义字符的 URI。 此方法为 URI 中的所有无效字符提供转义字符。

注释

不支持文件传输协议(FTP)。 如果提供的 assemblyFile URI 是 FTP 地址,则不会加载程序集。 不会引发异常。

assemblyFile 可以是绝对目录或相对于当前目录。

程序集可以加载到三个上下文中的一个,也可以在没有上下文的情况下加载程序集:

  • 加载上下文包含通过探测找到的程序集:在 GAC、托管运行时或应用程序域中的主机程序集存储中ApplicationBasePrivateBinPathLoad 方法的大多数重载都将程序集加载到此上下文中。

  • 从上下文加载的程序集包含用户为其提供了未包含在探测搜索的目录中的路径的程序集。 LoadFromCreateInstanceFrom以及 ExecuteAssembly 按路径加载的方法示例。

    请参阅 <loadFromRemoteSources> 从远程位置加载程序集。

  • 仅反射上下文包含使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法加载的程序集;无法执行这些上下文中的代码。

  • 如果用户生成或找到了程序集,则它没有任何上下文。 这适用于使用方法重载加载的 Load 程序集,该方法的重载指定包含程序集的字节数组,以及使用反射发出创建的暂时性动态程序集,而不是保存到磁盘。

从上下文加载允许从探测中不包含的路径加载程序集,但允许找到并加载该路径的依赖项,因为路径信息由上下文维护。

该方法 LoadFrom 具有以下缺点。 请考虑改用 Load

  • 如果已加载具有相同标识的程序集,即使指定了其他路径, LoadFrom 也会返回已加载的程序集。

  • 如果加载 LoadFrom了程序集,并且以后加载上下文中的程序集会尝试通过显示名称加载同一程序集,则加载尝试将失败。 对程序集进行反序列化时,可能发生这种情况。

  • 如果程序集已LoadFrom加载,并且探测路径包含具有相同标识的程序集,但可能会发生其他位置、一个InvalidCastExceptionMissingMethodException或其他意外行为。

  • LoadFrom 需要对指定路径的 FileIOPermissionAccess.ReadFileIOPermissionAccess.PathDiscoveryWebPermission

  • 如果存在 assemblyFile本机映像,则不使用它。 程序集不能作为非特定域加载。

是否将特定的权限授予程序集是基于证据的。 程序集和安全证据合并的规则如下所示:

  • 在没有参数的情况下LoadFrom使用Evidence方法时,会加载程序集,并带有加载程序提供的证据。

  • LoadFrom 方法与参数一 Evidence 起使用时,将合并一些证据。 作为参数提供的证据片段取代 LoadFrom 加载程序提供的证据片段。

  • 如果在同一程序集上多次调用此方法,但指定了不同的证据,则公共语言运行时不会引发此方法 FileLoadException ,因为无法确定不同证据规范的相等性和完整性。 首先成功的证据是使用的证据。

  • LoadFrom 方法与参数一起使用 Byte[] 以加载通用对象文件格式 (COFF) 图像时,将组合证据。 ZoneUrl 继承 Site 自调用程序集, Hash 并且 StrongName 取自 COFF 程序集。

  • LoadFrom 方法与参数一起使用 Byte[]Evidence 加载 COFF 图像时,仅使用提供的证据。 将忽略调用程序集的证据和 COFF 映像的证据。

适用于

LoadFrom(String)

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

加载给定其文件名或路径的程序集。

public:
 static System::Reflection::Assembly ^ LoadFrom(System::String ^ assemblyFile);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
public static System.Reflection.Assembly LoadFrom(string assemblyFile);
public static System.Reflection.Assembly LoadFrom(string assemblyFile);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")>]
static member LoadFrom : string -> System.Reflection.Assembly
static member LoadFrom : string -> System.Reflection.Assembly
Public Shared Function LoadFrom (assemblyFile As String) As Assembly

参数

assemblyFile
String

包含程序集清单的文件的名称或路径。

返回

加载的程序集。

属性

例外

assemblyFilenull

assemblyFile 找不到或尝试加载的模块未指定文件扩展名。

找不到的文件无法加载。

-或-

仅限 .NET Framework: assemblyFile 指定基于 <loadFromRemoteSources> 禁用的位置。

assemblyFile 不是当前加载的运行时的有效程序集;例如,64 位进程中的 32 位程序集。

如果未指定以“file://”开头的代码库,则不需要 WebPermission

参数 assemblyFile 是空字符串(“)。

程序集名称超过系统定义的最大长度。

示例

以下示例加载给定其文件名或路径的程序集。

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

注解

此 API 存在于 .NET Core 中,以保持兼容性,但建议改用 AssemblyLoadContext 重载。

程序集将加载到默认 AssemblyLoadContext 中。 有关程序集加载的详细信息,请参阅 托管程序集加载算法

参数 assemblyFile 必须引用不带转义字符的 URI。 此方法为 URI 中的所有无效字符提供转义字符。

注释

仅限 .NET Framework:不支持文件传输协议(FTP)。 如果提供的 assemblyFile URI 是 FTP 地址,则不会加载程序集。 不会引发异常。

assemblyFile 可以是绝对目录或相对于当前目录。

仅限 .NET Framework: 程序集可以加载到三个上下文中的一个,也可以在没有上下文的情况下加载程序集:

  • 加载上下文包含通过探测找到的程序集:在 GAC、托管运行时或应用程序域中的主机程序集存储中ApplicationBasePrivateBinPathLoad 方法的大多数重载都将程序集加载到此上下文中。

  • 从上下文加载的程序集包含用户为其提供了未包含在探测搜索的目录中的路径的程序集。 它还允许找到并加载该路径的依赖项,因为路径信息由上下文维护。 LoadFromCreateInstanceFrom以及 ExecuteAssembly 按路径加载的方法示例。

    请参阅 <loadFromRemoteSources> 从远程位置加载程序集。

  • 仅反射上下文包含使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法加载的程序集;无法执行这些上下文中的代码。

  • 如果用户生成或找到了程序集,则它没有任何上下文。 这适用于使用方法重载加载的 Load 程序集,该方法的重载指定包含程序集的字节数组,以及使用反射发出创建的暂时性动态程序集,而不是保存到磁盘。

该方法 LoadFrom 具有以下缺点。 请考虑改用 Load

  • 如果在从上下文中加载了具有相同标识的程序集,则即使指定了其他路径, LoadFrom 也会返回加载的程序集。

  • 即使加载上下文中存在具有相同标识的程序集,也可以从上下文中加载程序集。 两个程序集之间的互操作性将不起作用,从而导致错误,例如InvalidCastExceptionMissingMethodException,或其他意外行为。

  • 使用探测路径中的位置进行调用 LoadFrom 将在加载上下文中加载程序集,而不是加载自加载上下文中。

  • 如果将标识通过绑定重定向策略传递的 LoadFrom程序集文件传递给该策略,则会应用该策略,并且将从加载上下文中的探测路径加载程序集。

  • 如果在加载上下文中加载程序集,然后在加载上下文中的程序集尝试通过显示名称加载同一程序集,则加载尝试将失败。 对程序集进行反序列化时,可能发生这种情况。

  • LoadFrom 需要对指定路径的 FileIOPermissionAccess.ReadFileIOPermissionAccess.PathDiscoveryWebPermission

  • 如果存在 assemblyFile本机映像,则不使用它。 程序集不能作为非特定域加载。

适用于

LoadFrom(String, Evidence)

注意

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(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

参数

assemblyFile
String

包含程序集清单的文件的名称或路径。

securityEvidence
Evidence

加载程序集的证据。

返回

加载的程序集。

属性

例外

assemblyFilenull

assemblyFile 找不到或尝试加载的模块未指定文件扩展名。

找不到的文件无法加载。

-或-

securityEvidence 不明确,确定为无效。

-或-

assemblyFile 指定基于 <loadFromRemoteSources> 禁用的位置。

assemblyFile 不是当前加载的运行时的有效程序集;例如,64 位进程中的 32 位程序集。

如果未指定以“file://”开头的代码库,则不需要 WebPermission

参数 assemblyFile 是空字符串(“)。

程序集名称超过系统定义的最大长度。

注解

参数 assemblyFile 必须引用不带转义字符的 URI。 此方法为 URI 中的所有无效字符提供转义字符。

注释

不支持文件传输协议(FTP)。 如果提供的 assemblyFile URI 是 FTP 地址,则不会加载程序集。 不会引发异常。

assemblyFile 可以是绝对目录或相对于当前目录。

程序集可以加载到三个上下文中的一个,也可以在没有上下文的情况下加载程序集:

  • 加载上下文包含通过探测找到的程序集:在 GAC、托管运行时或应用程序域中的主机程序集存储中ApplicationBasePrivateBinPathLoad 方法的大多数重载都将程序集加载到此上下文中。

  • 从上下文加载的程序集包含用户为其提供了未包含在探测搜索的目录中的路径的程序集。 LoadFromCreateInstanceFrom以及 ExecuteAssembly 按路径加载的方法示例。

    请参阅 <loadFromRemoteSources> 从远程位置加载程序集。

  • 仅反射上下文包含使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法加载的程序集;无法执行这些上下文中的代码。

  • 如果用户生成或找到了程序集,则它没有任何上下文。 这适用于使用方法重载加载的 Load 程序集,该方法的重载指定包含程序集的字节数组,以及使用反射发出创建的暂时性动态程序集,而不是保存到磁盘。

从上下文加载允许从探测中不包含的路径加载程序集,但允许找到并加载该路径的依赖项,因为路径信息由上下文维护。

该方法 LoadFrom 具有以下缺点。 请考虑改用 Load

  • 如果已加载具有相同标识的程序集,即使指定了其他路径, LoadFrom 也会返回已加载的程序集。

  • 如果加载 LoadFrom了程序集,并且以后加载上下文中的程序集会尝试通过显示名称加载同一程序集,则加载尝试将失败。 对程序集进行反序列化时,可能发生这种情况。

  • 如果程序集已LoadFrom加载,并且探测路径包含具有相同标识的程序集,但可能会发生其他位置、一个InvalidCastExceptionMissingMethodException或其他意外行为。

  • LoadFrom 需要对指定路径的 FileIOPermissionAccess.ReadFileIOPermissionAccess.PathDiscoveryWebPermission

  • 如果存在 assemblyFile本机映像,则不使用它。 程序集不能作为非特定域加载。

是否将特定的权限授予程序集是基于证据的。 程序集和安全证据合并的规则如下所示:

  • 在没有参数的情况下LoadFrom使用Evidence方法时,会加载程序集,并带有加载程序提供的证据。

  • LoadFrom 方法与参数一 Evidence 起使用时,将合并一些证据。 作为参数提供的证据片段取代 LoadFrom 加载程序提供的证据片段。

  • 如果在同一程序集上多次调用此方法,但指定了不同的证据,则公共语言运行时不会引发此方法 FileLoadException ,因为无法确定不同证据规范的相等性和完整性。 首先成功的证据是使用的证据。

  • LoadFrom 方法与参数一起使用 Byte[] 以加载通用对象文件格式 (COFF) 图像时,将组合证据。 ZoneUrl 继承 Site 自调用程序集, Hash 并且 StrongName 取自 COFF 程序集。

  • LoadFrom 方法与参数一起使用 Byte[]Evidence 加载 COFF 图像时,仅使用提供的证据。 将忽略调用程序集的证据和 COFF 映像的证据。

另请参阅

适用于