Επεξεργασία

Κοινή χρήση μέσω


Assembly.Load Method

Definition

Loads an assembly.

Overloads

Load(Byte[], Byte[], SecurityContextSource)

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly, optionally including symbols and specifying the source for the security context.

Load(Byte[], Byte[], Evidence)
Obsolete.

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly, optionally including symbols and evidence for the assembly.

Load(String, Evidence)
Obsolete.

Loads an assembly given its display name and using the supplied evidence.

Load(AssemblyName, Evidence)
Obsolete.

Loads an assembly given its AssemblyName. The assembly is loaded using the supplied evidence.

Load(Byte[])

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly.

Load(String)

Loads an assembly with the specified name.

Load(AssemblyName)

Loads an assembly given its AssemblyName.

Load(Byte[], Byte[])

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly, optionally including symbols for the assembly.

Load(Byte[], Byte[], SecurityContextSource)

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly, optionally including symbols and specifying the source for the security context.

public:
 static System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore, System::Security::SecurityContextSource securityContextSource);
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, System.Security.SecurityContextSource securityContextSource);
static member Load : byte[] * byte[] * System.Security.SecurityContextSource -> System.Reflection.Assembly
Public Shared Function Load (rawAssembly As Byte(), rawSymbolStore As Byte(), securityContextSource As SecurityContextSource) As Assembly

Parameters

rawAssembly
Byte[]

A byte array that is a COFF-based image containing an emitted assembly.

rawSymbolStore
Byte[]

A byte array that contains the raw bytes representing the symbols for the assembly.

securityContextSource
SecurityContextSource

The source of the security context.

Returns

The loaded assembly.

Exceptions

rawAssembly is null.

rawAssembly is not a valid assembly for the currently loaded runtime.

The value of securityContextSource is not one of the enumeration values.

Remarks

The assembly is loaded into the application domain of the caller using the specified source for the security context. If rawSymbolStore was specified, the raw bytes that represent the symbols for the assembly are also loaded.

Applies to

Load(Byte[], Byte[], Evidence)

Caution

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

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly, optionally including symbols and evidence for the assembly.

public:
 static System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore, System::Security::Policy::Evidence ^ securityEvidence);
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, 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 Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence);
static member Load : byte[] * byte[] * 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 Load which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
static member Load : byte[] * byte[] * System.Security.Policy.Evidence -> System.Reflection.Assembly
Public Shared Function Load (rawAssembly As Byte(), rawSymbolStore As Byte(), securityEvidence As Evidence) As Assembly

Parameters

rawAssembly
Byte[]

A byte array that is a COFF-based image containing an emitted assembly.

rawSymbolStore
Byte[]

A byte array that contains the raw bytes representing the symbols for the assembly.

securityEvidence
Evidence

Evidence for loading the assembly.

Returns

The loaded assembly.

Attributes

Exceptions

rawAssembly is null.

rawAssembly is not a valid assembly for the currently loaded runtime.

An assembly or module was loaded twice with two different evidences.

securityEvidence is not null. By default, legacy CAS policy is not enabled in the .NET Framework 4; when it is not enabled, securityEvidence must be null.

Remarks

This method overload always creates a new Assembly object in its own isolated load context.

The assembly is loaded using the supplied evidence. The raw bytes representing the symbols for the assembly are also loaded.

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 Load method with no Evidence parameter, the assembly is loaded with the evidence that the loader supplies.

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

  • When you use a Load method overload with a Byte[] parameter to load a COFF image, evidence is inherited from the calling assembly.

  • When you use a Load 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 are ignored.

If you call the Load 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.

Applies to

Load(String, Evidence)

Caution

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

Loads an assembly given its display name and using the supplied evidence.

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

Parameters

assemblyString
String

The display name of the assembly.

assemblySecurity
Evidence

Evidence for loading the assembly.

Returns

The loaded assembly.

Attributes

Exceptions

assemblyString is null.

assemblyString is not found.

assemblyString is not a valid assembly for the currently loaded runtime.

A file that was found could not be loaded.

-or-

An assembly or module was loaded twice with two different evidences.

Remarks

FileLoadException is thrown if assemblyString specifies the full assembly name, and the first assembly that matches the simple name has a different version, culture, or public key token. The loader does not continue probing for other assemblies that match the simple name.

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 Load method with no Evidence parameter, the assembly is loaded with the evidence that the loader supplies.

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

  • When you use a Load method overload with a Byte[] parameter to load a common object file format (COFF) image, evidence is inherited from the calling assembly.

  • When you use a Load 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.

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.

In the .NET Framework version 2.0, processor architecture is added to assembly identity, and can be specified as part of assembly name strings. For example, "ProcessorArchitecture=msil". However, the recommended way to specify an assembly name is to create an AssemblyName object and pass it to an appropriate overload of the Load method. See AssemblyName.ProcessorArchitecture.

See also

Applies to

Load(AssemblyName, Evidence)

Caution

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

Loads an assembly given its AssemblyName. The assembly is loaded using the supplied evidence.

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

Parameters

assemblyRef
AssemblyName

The object that describes the assembly to be loaded.

assemblySecurity
Evidence

Evidence for loading the assembly.

Returns

The loaded assembly.

Attributes

Exceptions

assemblyRef is null.

assemblyRef is not found.

assemblyRef is not a valid assembly for the currently loaded runtime.

An assembly or module was loaded twice with two different evidences.

-or-

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

Remarks

FileLoadException is thrown if assemblyRef specifies the full assembly name, and the first assembly that matches the simple name has a different version, culture, or public key token. The loader does not continue probing for other assemblies that match the simple name.

See <loadFromRemoteSources> for loading assemblies from remote locations.

Note

Do not use an AssemblyName with only the CodeBase property set. The CodeBase property does not supply any elements of the assembly identity (such as name or version), so loading does not occur according to load-by-identity rules, as you would expect from the Load method. Instead, the assembly is loaded using load-from rules. For information about the disadvantages of using the load-from context, see the Assembly.LoadFrom method overload or Best Practices for Assembly Loading.

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 Load method with no Evidence parameter, the assembly is loaded with the evidence that the loader supplies.

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

  • When you use a Load method overload with a Byte[] parameter to load a common object file format (COFF) image, evidence is inherited from the calling assembly.

  • When you use a Load 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.

Note

If both the AssemblyName.Name property and the AssemblyName.CodeBase property are set, the first attempt to load the assembly uses the display name (including version, culture, and so on, as returned by the Assembly.FullName property). If the file is not found, CodeBase is used to search for the assembly. If the assembly is found using CodeBase, the display name is matched against the assembly. If the match fails, a FileLoadException is thrown.

If you call the Load 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.

See also

Applies to

Load(Byte[])

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

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly.

public:
 static System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly);
public static System.Reflection.Assembly Load (byte[] rawAssembly);
static member Load : byte[] -> System.Reflection.Assembly
Public Shared Function Load (rawAssembly As Byte()) As Assembly

Parameters

rawAssembly
Byte[]

A byte array that is a COFF-based image containing an emitted assembly.

Returns

The loaded assembly.

Exceptions

rawAssembly is null.

rawAssembly is not a valid assembly for the currently loaded runtime.

Remarks

This method overload always creates a new Assembly object in its own isolated load context.

.NET Framework only: The trust level of an assembly that is loaded by using this method is the same as the trust level of the calling assembly. To load an assembly from a byte array with the trust level of the application domain, use the Load(Byte[], Byte[], SecurityContextSource) method overload. For more information about the use of evidence with overloads of the Load method that take byte arrays, see the Load(Byte[], Byte[], Evidence) method overload.

Applies to

Load(String)

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

Loads an assembly with the specified name.

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

Parameters

assemblyString
String

The long or short form of the assembly name.

Returns

The loaded assembly.

Exceptions

assemblyString is null.

assemblyString is a zero-length string.

assemblyString is not found.

A file that was found could not be loaded.

assemblyString is not a valid assembly for the currently loaded runtime.

Examples

The following example loads an assembly given its fully qualified name, and lists all the types contained in the specified assembly. For information about how to obtain the fully qualified assembly name, see Assembly Names.

using namespace System;
using namespace System::Collections;
using namespace System::Reflection;
int main()
{
    // You must supply a valid fully qualified assembly name.            
    Assembly^ SampleAssembly = Assembly::Load
        ( "SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3" );
    array<Type^>^ Types = SampleAssembly->GetTypes();
    
    // Display all the types contained in the specified assembly.
    IEnumerator^ myEnum = Types->GetEnumerator();
    Type^ oType;
    while ( myEnum->MoveNext() )
    {
        oType = safe_cast<Type^>(myEnum->Current);
        Console::WriteLine( oType->Name );
    }
}
using System;
using System.Reflection;

class Class1
{
    public static void Main()
    {
        // You must supply a valid fully qualified assembly name.
        Assembly SampleAssembly = Assembly.Load
            ("SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3");
        // Display all the types contained in the specified assembly.
        foreach (Type oType in SampleAssembly.GetTypes()) {
            Console.WriteLine(oType.Name);
        }
    }
}
Imports System.Reflection

Class Class1
    Public Shared Sub Main()
        ' You must supply a valid fully qualified assembly name.            
        Dim SampleAssembly As [Assembly] = _
            [Assembly].Load("SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3")
        Dim oType As Type
        ' Display all the types contained in the specified assembly.
        For Each oType In SampleAssembly.GetTypes()
            Console.WriteLine(oType.Name)
        Next oType
    End Sub	'LoadSample
End Class

Remarks

In .NET Core/5+, the target assembly is loaded into the current AssemblyLoadContext. For more information on assembly loading, see Managed assembly loading algorithm.

To load the correct assembly, it's recommended to call the Load method by passing the long form of the assembly name. The long form of an assembly name consists of its simple name (such as "System" for the System.dll assembly) along with its version, culture, public key token, and optionally its processor architecture. It corresponds to the assembly's FullName property. The following example illustrates the use of a long name to load the System.dll assembly for the .NET Framework 4:

using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      string longName = "system, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
      Assembly assem = Assembly.Load(longName);
      if (assem == null)
         Console.WriteLine("Unable to load assembly...");
      else
         Console.WriteLine(assem.FullName);
   }
}
// The example displays the following output:
//        system, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim longName As String = "system, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      Dim assem As Assembly = Assembly.Load(longName)
      If assem Is Nothing Then
         Console.WriteLine("Unable to load assembly...")
      Else
         Console.WriteLine(assem.FullName)
      End If
   End Sub
End Module
' The example displays the following output:
'       system, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

FileLoadException is thrown if assemblyString specifies the full assembly name, and the first assembly that matches the simple name has a different version, culture, or public key token. The loader does not continue probing for other assemblies that match the simple name.

In the .NET Framework version 2.0, processor architecture is added to assembly identity, and can be specified as part of assembly name strings. For example, "ProcessorArchitecture=msil". However, the recommended way to specify an assembly name is to create an AssemblyName object and pass it to an appropriate overload of the Load method. See AssemblyName.ProcessorArchitecture.

See also

Applies to

Load(AssemblyName)

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

Loads an assembly given its AssemblyName.

public:
 static System::Reflection::Assembly ^ Load(System::Reflection::AssemblyName ^ assemblyRef);
public static System.Reflection.Assembly Load (System.Reflection.AssemblyName assemblyRef);
static member Load : System.Reflection.AssemblyName -> System.Reflection.Assembly
Public Shared Function Load (assemblyRef As AssemblyName) As Assembly

Parameters

assemblyRef
AssemblyName

The object that describes the assembly to be loaded.

Returns

The loaded assembly.

Exceptions

assemblyRef is null.

assemblyRef is not found.

A file that was found could not be loaded.

-or-

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

Note: In .NET for Windows Store apps or the Portable Class Library, catch the base class exception, IOException, instead.

assemblyRef is not a valid assembly for the currently loaded runtime.

Examples

The following example instantiates an AssemblyName object and uses it to load the sysglobal.dll assembly. The example then displays the full name of the assembly's public types.

using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      String fullName = "sysglobl, Version=4.0.0.0, Culture=neutral, " +
                        "PublicKeyToken=b03f5f7f11d50a3a, processor architecture=MSIL";
      var an = new AssemblyName(fullName);
      var assem = Assembly.Load(an);
      Console.WriteLine("Public types in assembly {0}:", assem.FullName);
      foreach (var t in assem.GetTypes())
         if (t.IsPublic)
            Console.WriteLine("   {0}", t.FullName);
   }
}
// The example displays the following output:
//   Public types in assembly sysglobl, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a:
//      System.Globalization.CultureAndRegionInfoBuilder
//      System.Globalization.CultureAndRegionModifiers
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim fullName As String = "sysglobl, Version=4.0.0.0, Culture=neutral, " +
                               "PublicKeyToken=b03f5f7f11d50a3a, processor architecture=MSIL"
      Dim an As New AssemblyName(fullName)
      Dim assem As Assembly = Assembly.Load(an)
      Console.WriteLine("Public types in assembly {0}:", assem.FullName)
      For Each t As Type in assem.GetTypes()
         If t.IsPublic Then Console.WriteLine("   {0}", t.FullName)
      Next
   End Sub
End Module
' The example displays the following output:
'   Public types in assembly sysglobl, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a:
'      System.Globalization.CultureAndRegionInfoBuilder
'      System.Globalization.CultureAndRegionModifiers

Remarks

In .NET Core/.NET 5+, the target assembly will be loaded into the current AssemblyLoadContext. For more information on assembly loading, see Managed assembly loading algorithm.

Note

.NET Framework only: For information about loading assemblies from remote locations, see <loadFromRemoteSources>.

Note

.NET Framework only: Do not use an AssemblyName with only the CodeBase property set. The CodeBase property does not supply any elements of the assembly identity (such as name or version), so loading does not occur according to load-by-identity rules, as you would expect from the Load method. Instead, the assembly is loaded using load-from rules. For information about the disadvantages of using the load-from context, see the Assembly.LoadFrom method overload or Best Practices for Assembly Loading.

Note

.NET Framework only: If both the AssemblyName.Name property and the AssemblyName.CodeBase property are set, the first attempt to load the assembly uses the display name (including version, culture, and so on, as returned by the Assembly.FullName property). If the file is not found, CodeBase is used to search for the assembly. If the assembly is found using CodeBase, the display name is matched against the assembly. If the match fails, a FileLoadException is thrown.

See also

Applies to

Load(Byte[], Byte[])

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

Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly, optionally including symbols for the assembly.

public:
 static System::Reflection::Assembly ^ Load(cli::array <System::Byte> ^ rawAssembly, cli::array <System::Byte> ^ rawSymbolStore);
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[]? rawSymbolStore);
public static System.Reflection.Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore);
static member Load : byte[] * byte[] -> System.Reflection.Assembly
Public Shared Function Load (rawAssembly As Byte(), rawSymbolStore As Byte()) As Assembly

Parameters

rawAssembly
Byte[]

A byte array that is a COFF-based image containing an emitted assembly.

rawSymbolStore
Byte[]

A byte array that contains the raw bytes representing the symbols for the assembly.

Returns

The loaded assembly.

Exceptions

rawAssembly is null.

rawAssembly is not a valid assembly for the currently loaded runtime.

Remarks

In .NET Core/5+, the target assembly is loaded into the current AssemblyLoadContext. For more information on assembly loading, see Managed assembly loading algorithm.

This method overload always creates a new Assembly object in its own isolated load context.

.NET Framework only: The trust level of an assembly that is loaded by using this method is the same as the trust level of the calling assembly. To load an assembly from a byte array with the trust level of the application domain, use the Load(Byte[], Byte[], SecurityContextSource) method overload. For more information about the use of evidence with overloads of the Load method that take byte arrays, see the Load(Byte[], Byte[], Evidence) method overload.

Applies to