Static News Archive



   (8-Feb-09 15:03)  Using Assembly Resolve
I have a handful of standalone executables that rely on shared code.

So, instead of duplicating the code internally, I add the required libraries as resources inside my executable.

Then, to load them dynamically, I attach a ResolveEventHandler to the CurrentDomain object:



AppDomain.CurrentDomain.AssemblyResolve = new ResolveEventHandler(
delegate(object sender, ResolveEventArgs REArgs)
{
Console.WriteLine("Trying to resolve [" REArgs.Name "]");
// Get the name of our assembly we are trying to resolve
string[] assemblyInfo = REArgs.Name.Split(new char[] { ',' });
ResourceSet oSet = Resources.
ResourceManager.GetResourceSet(
CultureInfo.CurrentCulture, true, true);
IDictionaryEnumerator lEnum = oSet.GetEnumerator();

// A buffer to hold our resource
byte[] dllData = new byte[0];
// Enumerate through all of our resources until we find our one
while (lEnum.MoveNext())
{
// If this resource name matches our requested assembly...
if (lEnum.Key.ToString() == assemblyInfo[0])
{
// ...Assign the bytes of this resources, and break
dllData = (byte[])lEnum.Value;
break;
}
}

if (dllData.Length > 0)
{
Console.WriteLine("Embedded dll [" REArgs.Name "] found");
return Assembly.Load(dllData);
}
else
{
Console.WriteLine("Could not find embedded dll [" REArgs.Name "]...");
return null;
}
}



One thing to watch out for is initialisation of your classes in you shared resources before you call this.
So, if above your static void Main method you have any global variables, make sure you do not initialise them:

private static ObjectFromSharedLib oObj = null;


Instead, initialise the variables after your ResolveEventHandler has been assigned.

-Rob-
Post a comment     

<-  (15-Jan-09 21:49)  The time spent maki... (1-Mar-09 18:58)  Javascript Memory le...  ->













(sitemap)