Mert Serimer
Mert Serimer

Reputation: 1245

C# Change using namespace reference dll path dynamically IN .NET LIBRARY PROJECT

I have a class library .net project(no config,no exe file) and I have something like this;

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Web.UI.WebControls;
using eBAControls; *<-- I want this dll to be loaded from a path in runtime.*

I have to make copyLocal false for future reasons thus i need to be able to refer the dll in runtime. Other 3rd party dlls which are in same folder with this current dll do not cause any problems but eBAControls dll is not in the same folder with this current dll thus it gives the error: Could not load file or assembly 'eBAControls...

I can not also use reflection for that for some coding and performance reasons thus it needs to be added to references...

Shortly what i want is

using eBAControls = @"somepath"; <- that is set dynamically for ex while one of my methods are being executed etc...

Upvotes: 0

Views: 1161

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 88971

You can load the assembly from wherever you like if you place an event handler on AppDomain.AssemblyResolve. See Resolving Assembly Loads

Here's a blog post that has the essentials. The key is timing. You have to attach the event before the first method referencing the assembly is JITted. https://blogs.msdn.microsoft.com/dbrowne/2014/06/25/how-to-load-an-assembly-in-a-ssis-script-task-that-isnt-in-the-gac/

Upvotes: 3

Related Questions