The SLAR (vol2) on System.Assembly
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.
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
Comments
- Anonymous
October 13, 2005
Hi Brad,
Would you be able to recommend a SLAR-like resource that covers parts of the framework that neither volume of the SLAR covers due to them being OS specific? I'm thinking about the System.Diagnostics namespace, in particular.
Cheers,
Dave - Anonymous
October 13, 2005
Brad,
It seems the last time you posted, I got the full post on the feed. Now I just get the title. If that's not a bug, I encourage you to go back to full posts. It makes it easier to read your stuff!
Thanks for posting!
Walt - Anonymous
October 13, 2005
Walt -- Not sure what the issue is.. but I agree with you, full posts in the feed are much nicer.. I will ask the MSDN support folks to look into it.
Dave -- Sorry, I don't have a recommendation... maybe another reader does... - Anonymous
October 14, 2005
Walter, I did get the full post in the feed. (Sharpreader). It may be possible the stuff copy pasted from Office to the blog is messing up the feed xml somehow and your RSS aggregator can't handle fully? - Anonymous
June 01, 2009
PingBack from http://woodtvstand.info/story.php?id=5321