Assembly required!

There’s a good article in the May issue of MSDN magazine regarding the Assembly binder, describing how it behaves when you mix LoadFrom and Load. The binder takes care of ensuring that if you do a LoadFrom on an assembly that would be found in the directory structures that are scanned by Load, then a subsequent Load finds the same assembly. It achieves this by having two caches, one for Load and one for LoadFrom, and promotes entries from the LoadFrom cache into the Load cache if they meet the criterion above.

AppDomain current = AppDomain.CurrentDomain;

bool loadFirst = Convert.ToBoolean(Console.ReadLine());
string loadFromAssemblyPath = Console.ReadLine();
string privatePath = Console.ReadLine();

if (!String.IsNullOrEmpty(privatePath))
{
  current.AppendPrivatePath(privatePath);
}

Assembly loadFromAssembly = null;
Assembly loadAssembly = null;

Console.WriteLine("AppBase: " + current.BaseDirectory);
Console.WriteLine("Private: " + current.RelativeSearchPath);

foreach (bool pass in new bool[] { true, false })
{
  if (loadFirst == pass)
  {
    loadFromAssembly =
Assembly.LoadFrom(loadFromAssemblyPath);
    Console.WriteLine("LoadFrom: " + loadFromAssembly.Location);
  }
  else
  {
    loadAssembly =
Assembly.Load("ClassLibrary1");
    Console.WriteLine("Load :" + loadAssembly.Location);
  }
}

Console.WriteLine("Assemblies equal: " + (loadAssembly == loadFromAssembly));

Taking a ClassLibrary1.dll and putting it into c:temp and c:temp2, we get the following results:

AppBase: C:temp
Private:
LoadFrom: C:tempClassLibrary1.dll
Load    :C:tempClassLibrary1.dll
Assemblies equal: True

AppBase: C:temp
Private:
Load    :C:tempClassLibrary1.dll
LoadFrom: C:tempClassLibrary1.dll
Assemblies equal: True

AppBase: C:temp
Private:
LoadFrom: c:temp2ClassLibrary1.dll
Load    :C:tempClassLibrary1.dll
Assemblies equal: False

AppBase: C:temp
Private:
Load    :C:tempClassLibrary1.dll
LoadFrom: c:temp2ClassLibrary1.dll
Assemblies equal: False

Putting ClassLibrary1.dll in c:temp, c:tempprivate and c:temp2

AppBase: C:temp
Private: private
LoadFrom: C:tempClassLibrary1.dll
Load    :C:tempClassLibrary1.dll
Assemblies equal: True

AppBase: C:temp
Private: private
LoadFrom: c:tempprivateClassLibrary1.dll
Load    :C:tempClassLibrary1.dll
Assemblies equal: False

Putting ClassLibrary1.dll in c:tempprivate and c:temp2

AppBase: C:temp
Private: private
LoadFrom: C:tempprivateClassLibrary1.dll
Load    :C:tempprivateClassLibrary1.dll
Assemblies equal: True

AppBase: C:temp
Private: private
Load    :C:tempprivateClassLibrary1.dll
LoadFrom: C:tempprivateClassLibrary1.dll
Assemblies equal: True

Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s