Continuing in the series sharing some of the information in the .NET Framework Standard Library Annotated Reference Vol 1 and .NET Framework Standard Library Annotated Reference Vol 2 with some information on Assembly.

 

SC

Calling Load(AssemblyName) is not necessarily the same as calling Load(String). If the

AssemblyName.CodeBase is not set, then they do the same thing. So, if you’ve set the

AssemblyName.Name, CultureInfo, public key token/public key and/or Version

properties, it would be the same as if you had specified those properties in a String (as a

display name) and passed that to Load(String).

However, if the AssemblyName.CodeBase is set, but the AssemblyName.Name is not, then

it’s the same as calling Assembly.LoadFrom() on that AssemblyName.CodeBase. When

both the AssemblyName.CodeBase and the AssemblyName.Name are set, then the bind is

tried with all the given binding information, except the AssemblyName.CodeBase (so, again,

it’s just like calling Load(String)). If that succeeds, we’re done. But, if that fails, then the bind

is tried again with just the AssemblyName.CodeBase (just like LoadFrom()). If it fails again,

then of course the whole bind fails. But, if it succeeds, then we verify that the binding properties

in the AssemblyName match the found assembly. If they don’t match, a FileLoadException

will be thrown for HResult FUSION_E_REF_DEF_MISMATCH.

So, setting both the AssemblyName.CodeBase and the AssemblyName.Name is useful for

when you want to both load an assembly at a given path into the LoadFrom context, and verify

that it has the public key token, and so forth, that you expect. Of course, as described above

(and due to binding context rules), keep in mind that just because you call

Load(AssemblyName) with a CodeBase does not mean that it will be loaded from that path.

 

using System;

using System.Text;

using System.Reflection;

/// <summary>

/// This example shows how to generate ILDASM-like output for an assembly.

/// </summary>

public class AssemblySample

{

public static void Main()

{

Assembly assembly = Assembly.GetExecutingAssembly();

Console.WriteLine("ILDASM style output for '{0}'",

     assembly.GetName().Name);

Console.WriteLine("---------------------------------------------");

Console.WriteLine("// Metadata version: {0}",

    assembly.ImageRuntimeVersion);

foreach (AssemblyName refAssemblyName

     in assembly.GetReferencedAssemblies())

{

Console.WriteLine(".assembly extern {0}", refAssemblyName.Name);

Console.WriteLine("{");

Console.WriteLine(" .publickeytoken = ({0})",

    ByteArrayToHexString(refAssemblyName.GetPublicKeyToken()));

Console.WriteLine(" .ver {0}",

    refAssemblyName.Version.ToString().Replace('.', ':'));

Console.WriteLine("}");

}

Console.WriteLine(".assembly {0}",assembly.GetName().Name);

Console.WriteLine("{");

foreach (Attribute attrib in assembly.GetCustomAttributes(false))

{

   Console.WriteLine(" .custom instance {0}", attrib);

}

Console.WriteLine(" .hash algorithm {0}",

    assembly.GetName().HashAlgorithm);

Console.WriteLine(" .ver {0}",

    assembly.GetName().Version.ToString().Replace('.', ':'));

Console.WriteLine("}");

foreach (Module currentModule in assembly.GetModules())

{

    Console.WriteLine(".module {0}", currentModule.Name);

}

Console.WriteLine();

Console.WriteLine();

Console.WriteLine("Press Enter to continue");

Console.ReadLine();

}//end main

private static string ByteArrayToHexString(byte[] values)

{

StringBuilder hexString = new StringBuilder();

foreach (byte b in values)

{

hexString.AppendFormat("{0:X}", b);

hexString.Append(' ');

}

return hexString.ToString();

}

}

 

The output is

 

ILDASM style output for 'Assembly'

---------------------------------------------

// Metadata version: v1.1.4322

.assembly extern mscorlib

{

.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )

.ver 1:0:5000:0

}

.assembly Assembly

{

.custom instance System.Diagnostics.DebuggableAttribute

.hash algorithm SHA1

.ver 0:0:0:0

}

.module Assembly.exe

Press Enter to continue