Assembly.LoadFrom 方法

定義

載入組件。Loads an assembly.

多載

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

指定組件的檔案名稱或路徑、安全性辨識項、雜湊值和雜湊演算法,來載入組件。Loads an assembly given its file name or path, security evidence, hash value, and hash algorithm.

LoadFrom(String, Byte[], AssemblyHashAlgorithm)

指定組件的檔案名稱或路徑,雜湊值和雜湊演算法,來載入組件。Loads an assembly given its file name or path, hash value, and hash algorithm.

LoadFrom(String)

載入組件,指定其檔案名稱或路徑。Loads an assembly given its file name or path.

LoadFrom(String, Evidence)

載入組件,指定其檔案名稱或路徑,並提供安全性辨識項。Loads an assembly given its file name or path and supplying security evidence.

備註

從 .NET Framework 4 開始,預設會停用在從遠端位置載入之元件中執行程式碼的能力,而 LoadFrom 方法的呼叫會擲回 FileLoadExceptionStarting with .NET Framework 4, the ability to execute code in assemblies loaded from remote locations is disabled by default, and the call to the LoadFrom method throws a FileLoadException. 若要載入元件並執行其程式碼,您必須執行下列其中一項:To load the assembly and execute its code, you must either:

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

警告

此 API 現已淘汰。

指定組件的檔案名稱或路徑、安全性辨識項、雜湊值和雜湊演算法,來載入組件。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);
[System.Obsolete]
[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 https://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
Public Shared Function LoadFrom (assemblyFile As String, securityEvidence As Evidence, hashValue As Byte(), hashAlgorithm As AssemblyHashAlgorithm) As Assembly

參數

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

雜湊演算法,用於雜湊檔案和用來產生強式名稱 (Strong Name)。The hash algorithm used for hashing files and for generating the strong name.

傳回

載入的組件。The loaded assembly.

屬性

例外狀況

assemblyFilenullassemblyFile is null.

找不到assemblyFile ,或您想載入的模組並未指定檔案的副檔名。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- securityEvidence 並非模稜兩可,而且已判定為無效。The securityEvidence is not ambiguous and is determined to be invalid.

-或--or-

已停用在遠端組件中執行程式碼的能力。The ability to execute code in remote assemblies is disabled. 請參閱 <loadFromRemoteSources>See <loadFromRemoteSources>.

assemblyFile 不是有效的組件,例如 64 位元處理序中的 32 位元組件。assemblyFile is not a valid assembly; for example, a 32-bit assembly in a 64-bit process. 如需詳細資訊,請參閱例外狀況主題。See the exception topic for more information.

-或--or- 目前載入的是通用語言執行平台 2.0 版或更新版本,且已使用更新的版本來編譯 assemblyFileVersion 2.0 or later of the common language runtime is currently loaded and assemblyFile was compiled with a later version.

指定了並非以 "file://" 開頭的程式碼基底,且沒有必要的 WebPermissionA codebase that does not start with "file://" was specified without the required WebPermission.

assemblyFile 參數是空字串 ("")。The assemblyFile parameter is an empty string ("").

組件名稱超過系統定義的長度上限。The assembly name exceeds the system-defined maximum length.

備註

assemblyFile 參數必須參考不含 escape 字元的 URI。The assemblyFile parameter must refer to a URI without escape characters. 這個方法會為 URI 中的所有無效字元提供換用字元。This method supplies escape characters for all invalid characters in the URI.

注意

不支援檔案傳輸協定(FTP)。File transfer protocol (FTP) is not supported. 如果提供給 assemblyFile 的 URI 是 FTP 位址,則不會載入元件。If the URI supplied for assemblyFile is an FTP address, the assembly is not loaded. 不會有例外狀況擲回。No exception is thrown.

assemblyFile 可能是目前目錄的絕對或相對路徑,且元件會載入至呼叫端的網域。assemblyFile may be absolute or 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:

  • 載入內容包含探查所找到的元件:在 GAC 中,如果裝載執行時間,或在應用程式域的 ApplicationBasePrivateBinPath 中,則是在主機元件存放區中。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. Load 方法的大部分多載都會將組件載入此內容中。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. LoadFromCreateInstanceFromExecuteAssembly 是依路徑載入之方法的範例。LoadFrom, CreateInstanceFrom, and ExecuteAssembly are examples of methods that load by path.

    從 .NET Framework 4 開始,如果 assemblyFile 的 URI 指定遠端位置,則預設會停用元件載入,而 LoadFrom 方法則會擲回 FileLoadExceptionStarting with .NET Framework 4, if the URI of assemblyFile specifies a remote location, assembly loading is disabled by default, and the LoadFrom method throws a FileLoadException. 若要啟用從遠端位置載入的程式碼,您可以使用<loadFromRemoteSources>的 configuration 元素。To enable execution of code loaded from remote locations, you can use the <loadFromRemoteSources> configuration element.

  • 僅限反映的內容包含使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法載入的元件。無法執行這些內容中的程式碼。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. 這適用于使用 Load 方法(指定包含元件的位元組陣列)的多載載入的元件,以及針對使用反映發出所建立且不會儲存到磁片的暫時性動態元件。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.

LoadFrom 方法有下列缺點。The LoadFrom method has the following disadvantages. 請考慮改用 LoadConsider using Load instead.

  • 如果已載入具有相同身分識別的組件,LoadFrom 會傳回載入的組件,即使指定不同的路徑也是一樣。If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.

  • 如果使用 LoadFrom 載入組件,之後載入內容中的組件卻試圖根據顯示名稱載入相同組件,則載入嘗試會失敗。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.

  • 如果元件是以 LoadFrom載入,而且探查路徑包含具有相同身分識別但不同位置的元件,則可能會發生 InvalidCastExceptionMissingMethodException或其他非預期的行為。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 要求所指定路徑上的 FileIOPermissionAccess.ReadFileIOPermissionAccess.PathDiscoveryWebPermissionLoadFrom demands FileIOPermissionAccess.Read and FileIOPermissionAccess.PathDiscovery, or WebPermission, on the specified path.

  • 如果 assemblyFile的原生映射存在,則不會使用它。If a native image exists for assemblyFile, it is not used. 無法將元件載入為網域中性。The assembly cannot be loaded as domain neutral.

  • 在 .NET Framework 版本1.0 和1.1 中,不會套用原則。In the .NET Framework version 1.0 and 1.1, policy is not applied.

是否將某些使用權限授予組件則以辨識項 (Evidence) 為基礎。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:

  • 當您使用沒有 Evidence 參數的 LoadFrom 方法時,會使用載入器所提供的辨識項來載入元件。When you use a LoadFrom method with no Evidence parameter, the assembly is loaded with the evidence that the loader supplies.

  • 當您使用具有 Evidence 參數的 LoadFrom 方法時,會合並辨識項的片段。When you use a LoadFrom method with an Evidence parameter, pieces of evidence are merged. 提供做為 LoadFrom 方法引數的辨識項片段,會取代載入器所提供的辨識項部分。Pieces of evidence supplied as an argument to the LoadFrom method supersede pieces of evidence supplied by the loader.

  • 如果您在同一個元件上多次呼叫這個方法,但是指定了不同的辨識項,common language runtime 就不會擲回 FileLoadException,因為無法判斷不同辨識項規格的相等和完整性。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.

  • 當您使用具有 Byte[] 參數的 LoadFrom 方法載入通用物件檔案格式(COFF)影像時,會結合辨識項。When you use a LoadFrom method with a Byte[] parameter to load a common object file format (COFF) image, evidence is combined. ZoneUrlSite 繼承自呼叫元件,而且會從 COFF 元件取得 HashStrongNameZone, Url and Site are inherited from the calling assembly, and Hash and StrongName are taken from the COFF assembly.

  • 當您使用具有 Byte[] 參數的 LoadFrom 方法,並 Evidence 載入 COFF 影像時,只會使用所提供的辨識項。When you use a LoadFrom method with a Byte[] parameter and Evidence to load a COFF image, only the supplied evidence is used. 會忽略呼叫元件和 COFF 影像辨識項的辨識項。Evidence of the calling assembly and evidence of the COFF image is ignored.

安全性

SecurityPermission
載入具有辨識項的元件。to load an assembly with evidence. 相關聯的列舉: ControlEvidenceAssociated enumeration: ControlEvidence

FileIOPermission
讀取開頭為 "file://" 的 URI。for reading a URI that begins with "file://". 相關聯的列舉: ReadAssociated enumeration: Read

WebPermission
用於讀取不是以 "file://" 開頭的 URI。for reading a URI that does not begin with "file://".

LoadFrom(String, Byte[], 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);
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

檔案的名稱或路徑,包含組件的資訊清單。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

雜湊演算法,用於雜湊檔案和用來產生強式名稱 (Strong Name)。The hash algorithm used for hashing files and for generating the strong name.

傳回

載入的組件。The loaded assembly.

例外狀況

assemblyFilenullassemblyFile is null.

找不到 assemblyFile,或您想載入的模組並未指定檔案的副檔名。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-

已停用在遠端組件中執行程式碼的能力。The ability to execute code in remote assemblies is disabled. 請參閱 <loadFromRemoteSources>See <loadFromRemoteSources>.

assemblyFile 不是有效的組件,例如 64 位元處理序中的 32 位元組件。assemblyFile is not a valid assembly; for example, a 32-bit assembly in a 64-bit process. 如需詳細資訊,請參閱例外狀況主題。See the exception topic for more information.

-或--or- 用以編譯 assemblyFile 的通用語言執行平台,其版本比目前所載入的版本還要更新。assemblyFile was compiled with a later version of the common language runtime than the version that is currently loaded.

指定了並非以 "file://" 開頭的程式碼基底,且沒有必要的 WebPermissionA codebase that does not start with "file://" was specified without the required WebPermission.

assemblyFile 參數是空字串 ("")。The assemblyFile parameter is an empty string ("").

組件名稱超過系統定義的長度上限。The assembly name exceeds the system-defined maximum length.

備註

assemblyFile 參數必須參考不含 escape 字元的 URI。The assemblyFile parameter must refer to a URI without escape characters. 這個方法會為 URI 中的所有無效字元提供換用字元。This method supplies escape characters for all invalid characters in the URI.

注意

不支援檔案傳輸協定(FTP)。File transfer protocol (FTP) is not supported. 如果提供給 assemblyFile 的 URI 是 FTP 位址,則不會載入元件。If the URI supplied for assemblyFile is an FTP address, the assembly is not loaded. 不會有例外狀況擲回。No exception is thrown.

assemblyFile 可能是目前目錄的絕對或相對路徑,且元件會載入至呼叫端的網域。assemblyFile may be absolute or 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:

  • 載入內容包含探查所找到的元件:在全域組件快取中,如果裝載執行時間,或在應用程式域的 ApplicationBasePrivateBinPath 中,則位於主機元件存放區中。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. Load 方法的大部分多載都會將組件載入此內容中。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. LoadFromCreateInstanceFromExecuteAssembly 是依路徑載入之方法的範例。LoadFrom, CreateInstanceFrom, and ExecuteAssembly are examples of methods that load by path.

    從 .NET Framework 4 開始,如果 assemblyFile 的 URI 指定遠端位置,則預設會停用元件載入,而 LoadFrom 方法則會擲回 FileLoadExceptionStarting with .NET Framework 4, if the URI of assemblyFile specifies a remote location, assembly loading is disabled by default, and the LoadFrom method throws a FileLoadException. 若要啟用從遠端位置載入的程式碼,您可以使用<loadFromRemoteSources>的 configuration 元素。To enable execution of code loaded from remote locations, you can use the <loadFromRemoteSources> configuration element.

  • 僅限反映的內容包含使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法載入的元件。無法執行這些內容中的程式碼。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. 這適用于使用 Load 方法(指定包含元件的位元組陣列)的多載載入的元件,以及針對使用反映發出所建立且不會儲存到磁片的暫時性動態元件。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.

LoadFrom 方法有下列缺點。The LoadFrom method has the following disadvantages. 請考慮改用 LoadConsider using Load instead.

  • 如果已載入具有相同身分識別的組件,LoadFrom 會傳回載入的組件,即使指定不同的路徑也是一樣。If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.

  • 如果使用 LoadFrom 載入組件,之後載入內容中的組件卻試圖根據顯示名稱載入相同組件,則載入嘗試會失敗。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.

  • 如果元件是以 LoadFrom載入,而且探查路徑包含具有相同身分識別但不同位置的元件,則可能會發生 InvalidCastExceptionMissingMethodException或其他非預期的行為。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 要求所指定路徑上的 FileIOPermissionAccess.ReadFileIOPermissionAccess.PathDiscoveryWebPermissionLoadFrom demands FileIOPermissionAccess.Read and FileIOPermissionAccess.PathDiscovery, or WebPermission, on the specified path.

  • 如果 assemblyFile的原生映射存在,則不會使用它。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.

安全性

SecurityPermission
載入具有辨識項的元件。to load an assembly with evidence. 相關聯的列舉: ControlEvidenceAssociated enumeration: ControlEvidence

FileIOPermission
讀取開頭為 "file://" 的 URI。for reading a URI that begins with "file://". 相關聯的列舉: ReadAssociated enumeration: Read

WebPermission
用於讀取不是以 "file://" 開頭的 URI。for reading a URI that does not begin with "file://".

LoadFrom(String)

載入組件,指定其檔案名稱或路徑。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

參數

assemblyFile
String

檔案的名稱或路徑,包含組件的資訊清單。The name or path of the file that contains the manifest of the assembly.

傳回

載入的組件。The loaded assembly.

例外狀況

assemblyFilenullassemblyFile is null.

找不到assemblyFile ,或您想載入的模組並未指定檔案的副檔名。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 ability to execute code in remote assemblies is disabled. 請參閱 <loadFromRemoteSources>See <loadFromRemoteSources>.

assemblyFile 不是有效的組件,例如 64 位元處理序中的 32 位元組件。assemblyFile is not a valid assembly; for example, a 32-bit assembly in a 64-bit process. 如需詳細資訊,請參閱例外狀況主題。See the exception topic for more information.

-或--or- 目前載入的是通用語言執行平台 2.0 版或更新版本,且已使用更新的版本來編譯 assemblyFileVersion 2.0 or later of the common language runtime is currently loaded and assemblyFile was compiled with a later version.

指定了並非以 "file://" 開頭的程式碼基底,且沒有必要的 WebPermissionA codebase that does not start with "file://" was specified without the required WebPermission.

assemblyFile 參數是空字串 ("")。The assemblyFile parameter is an empty string ("").

組件名稱超過系統定義的長度上限。The assembly name exceeds the system-defined maximum length.

範例

下列範例會載入元件,指定其檔案名或路徑。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 

備註

assemblyFile 參數必須參考不含 escape 字元的 URI。The assemblyFile parameter must refer to a URI without escape characters. 這個方法會為 URI 中的所有無效字元提供換用字元。This method supplies escape characters for all invalid characters in the URI.

注意

不支援檔案傳輸協定(FTP)。File transfer protocol (FTP) is not supported. 如果提供給 assemblyFile 的 URI 是 FTP 位址,則不會載入元件。If the URI supplied for assemblyFile is an FTP address, the assembly is not loaded. 不會有例外狀況擲回。No exception is thrown.

assemblyFile 可能是目前目錄的絕對或相對路徑,且元件會載入至呼叫端的網域。assemblyFile may be absolute or 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:

  • 載入內容包含探查所找到的元件:在 GAC 中,如果裝載執行時間,或在應用程式域的 ApplicationBasePrivateBinPath 中,則是在主機元件存放區中。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. Load 方法的大部分多載都會將組件載入此內容中。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. LoadFromCreateInstanceFromExecuteAssembly 是依路徑載入之方法的範例。LoadFrom, CreateInstanceFrom, and ExecuteAssembly are examples of methods that load by path.

    從 .NET Framework 4 開始,如果 assemblyFile 的 URI 指定遠端位置,則預設會停用元件載入,而 LoadFrom 方法則會擲回 FileLoadExceptionStarting with .NET Framework 4, if the URI of assemblyFile specifies a remote location, assembly loading is disabled by default, and the LoadFrom method throws a FileLoadException. 若要啟用從遠端位置載入的程式碼,您可以使用<loadFromRemoteSources>的 configuration 元素。To enable execution of code loaded from remote locations, you can use the <loadFromRemoteSources> configuration element.

  • 僅限反映的內容包含使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法載入的元件。無法執行這些內容中的程式碼。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. 這適用于使用 Load 方法(指定包含元件的位元組陣列)的多載載入的元件,以及針對使用反映發出所建立且不會儲存到磁片的暫時性動態元件。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.

LoadFrom 方法有下列缺點。The LoadFrom method has the following disadvantages. 請考慮改用 LoadConsider using Load instead.

  • 如果已載入具有相同身分識別的組件,LoadFrom 會傳回載入的組件,即使指定不同的路徑也是一樣。If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.

  • 如果使用 LoadFrom 載入組件,之後載入內容中的組件卻試圖根據顯示名稱載入相同組件,則載入嘗試會失敗。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 de-serialized.

  • 如果元件是以 LoadFrom載入,而且探查路徑包含具有相同身分識別但不同位置的元件,則可能會發生 InvalidCastExceptionMissingMethodException或其他非預期的行為。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 要求所指定路徑上的 FileIOPermissionAccess.ReadFileIOPermissionAccess.PathDiscoveryWebPermissionLoadFrom demands FileIOPermissionAccess.Read and FileIOPermissionAccess.PathDiscovery, or WebPermission, on the specified path.

  • 如果 assemblyFile的原生映射存在,則不會使用它。If a native image exists for assemblyFile, it is not used. 無法將元件載入為網域中性。The assembly cannot be loaded as domain neutral.

  • 在 .NET Framework 版本1.0 和1.1 中,不會套用原則。In the .NET Framework version 1.0 and 1.1, policy is not applied.

安全性

FileIOPermission
讀取開頭為 "file://" 的 URI。for reading a URI that begins with "file://". 相關聯的列舉: ReadAssociated enumeration: Read

WebPermission
用於讀取不是以 "file://" 開頭的 URI。for reading a URI that does not begin with "file://".

LoadFrom(String, Evidence)

警告

此 API 現已淘汰。

載入組件,指定其檔案名稱或路徑,並提供安全性辨識項。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);
[System.Obsolete]
[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 https://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
Public Shared Function LoadFrom (assemblyFile As String, securityEvidence As Evidence) As Assembly

參數

assemblyFile
String

檔案的名稱或路徑,包含組件的資訊清單。The name or path of the file that contains the manifest of the assembly.

securityEvidence
Evidence

用於載入組件的辨識項。Evidence for loading the assembly.

傳回

載入的組件。The loaded assembly.

屬性

例外狀況

assemblyFilenullassemblyFile is null.

找不到assemblyFile ,或您想載入的模組並未指定檔案的副檔名。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- securityEvidence 並非模稜兩可,而且已判定為無效。The securityEvidence is not ambiguous and is determined to be invalid.

-或--or-

已停用在遠端組件中執行程式碼的能力。The ability to execute code in remote assemblies is disabled. 請參閱 <loadFromRemoteSources>See <loadFromRemoteSources>.

assemblyFile 不是有效的組件,例如 64 位元處理序中的 32 位元組件。assemblyFile is not a valid assembly; for example, a 32-bit assembly in a 64-bit process. 如需詳細資訊,請參閱例外狀況主題。See the exception topic for more information.

-或--or- 目前載入的是通用語言執行平台 2.0 版或更新版本,且已使用更新的版本來編譯 assemblyFileVersion 2.0 or later of the common language runtime is currently loaded and assemblyFile was compiled with a later version.

指定了並非以 "file://" 開頭的程式碼基底,且沒有必要的 WebPermissionA codebase that does not start with "file://" was specified without the required WebPermission.

assemblyFile 參數是空字串 ("")。The assemblyFile parameter is an empty string ("").

組件名稱超過系統定義的長度上限。The assembly name exceeds the system-defined maximum length.

備註

assemblyFile 參數必須參考不含 escape 字元的 URI。The assemblyFile parameter must refer to a URI without escape characters. 這個方法會為 URI 中的所有無效字元提供換用字元。This method supplies escape characters for all invalid characters in the URI.

注意

不支援檔案傳輸協定(FTP)。File transfer protocol (FTP) is not supported. 如果提供給 assemblyFile 的 URI 是 FTP 位址,則不會載入元件。If the URI supplied for assemblyFile is an FTP address, the assembly is not loaded. 不會有例外狀況擲回。No exception is thrown.

assemblyFile 可能是目前目錄的絕對或相對路徑,且元件會載入至呼叫端的網域。assemblyFile may be absolute or 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:

  • 載入內容包含探查所找到的元件:在 GAC 中,如果裝載執行時間,或在應用程式域的 ApplicationBasePrivateBinPath 中,則是在主機元件存放區中。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. Load 方法的大部分多載都會將組件載入此內容中。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. LoadFromCreateInstanceFromExecuteAssembly 是依路徑載入之方法的範例。LoadFrom, CreateInstanceFrom, and ExecuteAssembly are examples of methods that load by path.

    從 .NET Framework 4 開始,如果 assemblyFile 的 URI 指定遠端位置,則預設會停用元件載入,而 LoadFrom 方法則會擲回 FileLoadExceptionStarting with .NET Framework 4, if the URI of assemblyFile specifies a remote location, assembly loading is disabled by default, and the LoadFrom method throws a FileLoadException. 若要啟用從遠端位置載入的程式碼,您可以使用<loadFromRemoteSources>的 configuration 元素。To enable execution of code loaded from remote locations, you can use the <loadFromRemoteSources> configuration element.

  • 僅限反映的內容包含使用 ReflectionOnlyLoadReflectionOnlyLoadFrom 方法載入的元件。無法執行這些內容中的程式碼。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. 這適用于使用 Load 方法(指定包含元件的位元組陣列)的多載載入的元件,以及針對使用反映發出所建立且不會儲存到磁片的暫時性動態元件。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.

LoadFrom 方法有下列缺點。The LoadFrom method has the following disadvantages. 請考慮改用 LoadConsider using Load instead.

  • 如果已載入具有相同身分識別的組件,LoadFrom 會傳回載入的組件,即使指定不同的路徑也是一樣。If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.

  • 如果使用 LoadFrom 載入組件,之後載入內容中的組件卻試圖根據顯示名稱載入相同組件,則載入嘗試會失敗。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.

  • 如果元件是以 LoadFrom載入,而且探查路徑包含具有相同身分識別但不同位置的元件,則可能會發生 InvalidCastExceptionMissingMethodException或其他非預期的行為。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 要求所指定路徑上的 FileIOPermissionAccess.ReadFileIOPermissionAccess.PathDiscoveryWebPermissionLoadFrom demands FileIOPermissionAccess.Read and FileIOPermissionAccess.PathDiscovery, or WebPermission, on the specified path.

  • 如果 assemblyFile的原生映射存在,則不會使用它。If a native image exists for assemblyFile, it is not used. 無法將元件載入為網域中性。The assembly cannot be loaded as domain neutral.

  • 在 .NET Framework 版本1.0 和1.1 中,不會套用原則。In the .NET Framework version 1.0 and 1.1, policy is not applied.

是否將某些使用權限授予組件則以辨識項 (Evidence) 為基礎。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:

  • 當您使用沒有 Evidence 參數的 LoadFrom 方法時,會使用載入器所提供的辨識項來載入元件。When you use a LoadFrom method with no Evidence parameter, the assembly is loaded with the evidence that the loader supplies.

  • 當您使用具有 Evidence 參數的 LoadFrom 方法時,會合並辨識項的片段。When you use a LoadFrom method with an Evidence parameter, pieces of evidence are merged. 提供做為 LoadFrom 方法引數的辨識項片段,會取代載入器所提供的辨識項部分。Pieces of evidence supplied as an argument to the LoadFrom method supersede pieces of evidence supplied by the loader.

  • 如果您在同一個元件上多次呼叫這個方法,但是指定了不同的辨識項,common language runtime 就不會擲回 FileLoadException,因為無法判斷不同辨識項規格的相等和完整性。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.

  • 當您使用具有 Byte[] 參數的 LoadFrom 方法載入通用物件檔案格式(COFF)影像時,會結合辨識項。When you use a LoadFrom method with a Byte[] parameter to load a common object file format (COFF) image, evidence is combined. ZoneUrlSite 繼承自呼叫元件,而且會從 COFF 元件取得 HashStrongNameZone, Url and Site are inherited from the calling assembly, and Hash and StrongName are taken from the COFF assembly.

  • 當您使用具有 Byte[] 參數的 LoadFrom 方法,並 Evidence 載入 COFF 影像時,只會使用所提供的辨識項。When you use a LoadFrom method with a Byte[] parameter and Evidence to load a COFF image, only the supplied evidence is used. 會忽略呼叫元件和 COFF 影像辨識項的辨識項。Evidence of the calling assembly and evidence of the COFF image is ignored.

安全性

SecurityPermission
載入具有辨識項的元件。to load an assembly with evidence. 相關聯的列舉: ControlEvidenceAssociated enumeration: ControlEvidence

FileIOPermission
讀取開頭為 "file://" 的 URI。for reading a URI that begins with "file://". 相關聯的列舉: ReadAssociated enumeration: Read

WebPermission
用於讀取不是以 "file://" 開頭的 URI。for reading a URI that does not begin with "file://".

另請參閱

適用於