Export (0) Print
Expand All

Assembly.Load Method (String)

.NET Framework 1.1

Loads an assembly given the long form of its name.

[Visual Basic]
Overloads Public Shared Function Load( _
   ByVal assemblyString As String _
) As Assembly
[C#]
public static Assembly Load(
 string assemblyString
);
[C++]
public: static Assembly* Load(
 String* assemblyString
);
[JScript]
public static function Load(
   assemblyString : String
) : Assembly;

Parameters

assemblyString
The long form of the assembly name.

Return Value

The loaded assembly.

Exceptions

Exception Type Condition
ArgumentNullException assemblyString is a null reference (Nothing in Visual Basic).
FileNotFoundException assemblyString is not found.
BadImageFormatException assemblyFile is not a valid assembly.
SecurityException The caller does not have the required permission.

Remarks

The Load methods use the default load context, which records the assembly name and assembly instance information for the set of assemblies that is the transitive closure of the assemblies referenced by a managed application. The default load context applies to assemblies that are loaded with a Load method and that use a fully qualified assembly reference.

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 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 Load method with a Byte[] parameter and Evidence to load a common object file format (COFF) image, only the supplied evidence is used. Evidence of the calling assembly and evidence of the COFF image is ignored.

Reflecting on Managed Extensions for C++ executable files might throw a BadImageFormatException. This is most likely caused by C++ compiler stripping the relocation addresses or the .Reloc section from your executable file. To preserve the .reloc address for your C++ executable file, specify /fixed:no when you are linking.

Example

[Visual Basic, C#, C++] The following example loads an assembly given its fully qualified name, and lists all the types contained in the specified assembly. For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

[Visual Basic] 
Imports System
Imports System.Reflection

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

[C#] 
using System;
using System.Reflection;

class Class1
{
    public static void Main()
    {
        Assembly SampleAssembly;
        // You must supply a valid fully qualified assembly name here.            
        SampleAssembly = Assembly.Load("Assembly text name, Version, Culture, PublicKeyToken");
        Type[] Types = SampleAssembly.GetTypes();
        // Display all the types contained in the specified assembly.
        foreach (Type oType in Types)
        {
            Console.WriteLine(oType.Name.ToString());
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Reflection;

int main() 
{
   Assembly* SampleAssembly;
   // You must supply a valid fully qualified assembly name here.            
   SampleAssembly = Assembly::Load(S"Assembly text name, Version, Culture, PublicKeyToken");
   Type* Types[] = SampleAssembly->GetTypes();
   // Display all the types contained in the specified assembly.
   IEnumerator* myEnum = Types->GetEnumerator();
   while (myEnum->MoveNext())
   {
      Type* oType = __try_cast<Type*>(myEnum->Current);
      Console::WriteLine(oType->Name);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Assembly Class | Assembly Members | System.Reflection Namespace | Assembly.Load Overload List | AssemblyName | LoadFrom

Show:
© 2014 Microsoft