Assembly.Load
– AssemblyName (the full assembly name is known: name, version, cryptographic key pair(?), supported culture
– string (full assembly name is known and it is provided as a string)
– other overloads are obsoleted
 
Assembly.loadFrom
– string (file name or path)
– string, hash, hashAlgorithm
– use this to bypass the rule "you cannot load assemblies from directories above the directory where the executable
is located".
 
Assembly.LoadFile
– use this method to load and examine assemblies having the same identity, but are located in different paths.
 
The code below demonstrates that for GAC-ed assemblies, the assembly name must be completely provided (name, version, culture info, public key token).

using

System;

using

System.Collections.Generic;

using

System.Linq;

using

System.Text;

using

System.Reflection;

using

System.Globalization;

namespace

DemoAssemblyLoad

{

class Program

{

static void Main( string[] args )

{

// Case 1: Load assembly given its full path.

var name = new AssemblyName();

name.CodeBase =

@"C:projectsnps6-impsrcNPSbuildslibDevExpressDevExpress.Charts.v9.1.Core.dll";

var assembly = Assembly.Load( name );

// Allthough the assembly has been given a certain location, it has been

// retrieved from the GAC.

// Use a strong-named assembly that is not yet in the GAC.

name.CodeBase =

@"C:Program FilesMicrosoft SQL Server Compact Editionv3.5ToolsMicrosoft.SqlServerCe.Client.dll";

assembly =

Assembly.Load( name );

// The assembly has been properly loaded.

// Case 2: load assembly given its full path, but do not request for a version.

assembly =

Assembly.LoadFrom( @"C:Program FilesMicrosoft SQL Server Compact Editionv3.5ToolsMicrosoft.SqlServerCe.Client.dll" );

// Case 3: use LoadFile

assembly =

Assembly.LoadFile( @"C:Program FilesMicrosoft SQL Server Compact Editionv3.5ToolsMicrosoft.SqlServerCe.Client.dll" );

// All three situations work the same.

// Now request for an assembly knowing only the name.

name =

new AssemblyName();

name.Name =

"DevExpress.Charts.v9.1.Core";

try

{
assembly =

Assembly.Load( name );

}

catch(Exception e)

{

// Handle the exception.

}
name.Version =

new Version( "9.1.5.0" );

try

{
assembly =

Assembly.Load( name );

}

catch(Exception e)

{

// Handle the exception.

}
name.CultureInfo =

CultureInfo.InvariantCulture;

try

{
assembly =

Assembly.Load( name );

}

catch(Exception e)

{

// Handle the exception.

}
name.SetPublicKeyToken(

new byte[]{ 0xb8, 0x8d, 0x17, 0x54, 0xd7, 0x00, 0xe4, 0x9a }

);

try

{
assembly =

Assembly.Load( name );

}

catch(Exception e)

{

// Handle the exception.

}

// Well, this time the call succeeded.

// Conclusions:

// – Assembly.Load requires full name (name, version, culture, public key token).

// Another try: use only public key token.

name =

new AssemblyName();

name.SetPublicKeyToken(

new byte[] { 0xb8, 0x8d, 0x17, 0x54, 0xd7, 0x00, 0xe4, 0x9a }

);

try

{
assembly =

Assembly.Load( name );

}

catch(Exception e)

{

// Handle the exception.

}

// Does not work, the exception message is "the string cannot have 0 length".

// Add name.

name.Name =

@"DevExpress.Charts.v9.1.Core";

try

{
assembly =

Assembly.Load( name );

}

catch(Exception e)

{

// Handle the exception.

}

// Does not work, "file not found".

// Add version

name.Version =

new Version( "9.1.5.0" );

try

{
assembly =

Assembly.Load( name );

}

catch(Exception e)

{

// Handle the exception.

}

// Again, does not work.

// Skip adding culture info, the result is already known.

 
 
 

 

}

}
}

Advertisements