I don't know if it is intentional, but your foreach loop generates a path for each assembly, but you only ever load the last assembly, as the Assembly.Load is outside of the loop.
foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
{
if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
{
strTempAssmbPath = "D:\\Ezhirko\\SampleForm\\bin\\Common\\" +
args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
}
}
MyAssembly = Assembly.LoadFrom(strTempAssmbPath);
return MyAssembly;
My guess is that you are trying to load a specific assembly in this event.
I use this code in my own applications:
It might be easier to use.
This code was wrote with the intent of allowing me to resolve assemblies from Embedded resources within the DLL/APPLICATION itself.
If done right, you can even compress the dll's and have them decompressed at run time.
Note: this one might be easier to use, since it avoids that looping strategy you were using.
/// <summary>
/// This is the handler for failed assembly resolution attempts - when a failed resolve event fires, it will redirect the assembly lookup to internal
/// embedded resources. Not necessary for this method to ever be called by the user.
/// </summary>
/// <param name="sender">not important</param>
/// <param name="args">automatically provided</param>
/// <returns>returns the assembly once it is found</returns>
private static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
{
string[] toSplitWith = { "," };
//New improvement - Allows for ANY DLL to be resolved to a local memory stream.
bool bCompressed = true;
string sFileName = args.Name.Split(toSplitWith, StringSplitOptions.RemoveEmptyEntries)[0] + ".dll";
string sPath = "NameSpace.Resources.";
Assembly thisAssembly = Assembly.GetExecutingAssembly(); //Gets the executing Assembly
using (Stream input = thisAssembly.GetManifestResourceStream(sPath + sFileName)) // Acquire the dll from local memory/resources.
{
return input != null ? Assembly.Load(StreamToBytes(input)) : null; // More bitwise operators - if input not Null, return the Assembly, else return null.
}
}
While I use sPath = "NameSpace.Resources";
you could however point it at another folder location on your computer, and then just Assembly.LoadFrom, instead of worrying about the GetManifestResourceStream();
Also - about the resolve event being fired immediately.
If you want to ensure that the event gets fired 'after' the resolve handler has been setup, you need to embed the property/field within a subclass that gets instantiated after the resolve handler is setup.
If you have it as a property on the main form, those properties will try to be created at parent class creation time.
Even if they are null, the type is used, and if the type is used, it will try to pull the dll into memory at the same time.
So if you put the declaration into a sub class, only instantiating it after the resolve is setup, then it should not bother you about the DLL being missing.