Support
Joined: 18 Jul 2005 Posts: 731
|
Posted: Fri Jun 05, 2009 5:56 am Post subject: HOWTO: Load dependant assemblies from your namespace extension. |
|
|
If your namespace extension has dependant assemblies, you may get a FileNotFoundException when you try to browse your namespace extension. This is becuase the .Net runtime tries to find the dependant assemblies in the path of the host exe (example: "c:\windows" for Windows Explorer, ) but does not find them there. The solution is to handle the AppDomain.AssemblyResolve event in the static contructor of the class representing the root node of your namespace extension as follows:
[C#]
Code: | // MyRootNseFolder is the class representing the root node of the nse
static MyRootNseFolder()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.Contains("Dependant")) // .Net runtime is trying to load Dependant.dll assembly
{
// Load it from the same path as our nse dll and return it
string path = Assembly.GetExecutingAssembly().Location;
path = Path.GetDirectoryName(path);
path = Path.Combine(path, "Dependant.dll");
Assembly ret = Assembly.LoadFrom(path);
return ret;
}
return null;
} |
[VB.Net]
Code: | ' MyRootNseFolder is the class representing the root node of the nse
Shared Sub New()
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf CurrentDomain_AssemblyResolve
End Sub
Shared Function CurrentDomain_AssemblyResolve(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly
If (args.Name.Contains("Dependant")) Then ' .Net runtime is trying to load Dependant.dll assembly
'Load it from the same path as our nse dll and return it
Dim path As String = Assembly.GetExecutingAssembly().Location
path = System.IO.Path.GetDirectoryName(path)
path = System.IO.Path.Combine(path, "Dependant.dll")
Dim ret As Assembly = Assembly.LoadFrom(path)
Return ret
End If
Return Nothing
End Function |
|
|