Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Load Reference at runtime

I have following requirement,

  • I have C#/.Net console application, which refers to 'System.Data.Sqlite.dll'
  • 'System.Data.Sqlite.dll' is not registerd in GAC (i don't want to)
  • I don't want to keep it in the directory where the Executable(.exe) is kept.

  • How can i keep the 'System.Data.Sqlite.dll' file at some other directory , and load it safely at runtime, before its getting referenced in application code ?

like image 202
Palani Avatar asked Dec 01 '22 12:12

Palani


2 Answers

you can use manual assembly resolution to do this.

You need to provide a delegate to the AssemblyResolve event in the current AppDomain

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += assemblyResolver.ResolveEventHandler;

when the application has any assembly references that it can't resolve it will call this delegate to get the assembly resolved. You can then simply return the assembly requested from the delegate:

Assembly assembly = Assembly.LoadFrom (assemblyPath);
return assembly;

hope this helps

like image 133
Sam Holder Avatar answered Dec 20 '22 10:12

Sam Holder


I'm not sure why you want to do this again, it's a deviation from the best practices, But to answer your question: You could use

Assembly.LoadFrom Method (String path)

From the System.Reflection Namespace

& having the path in the app.config file

Assembly.LoadFrom on MSDN

like image 25
abhilash Avatar answered Dec 20 '22 10:12

abhilash