Unity Container Config question

I am using Unity DI container. In the config file I specify the following type:

<type type="Interfaces.ILogger,Interfaces" 
 mapTo = "ConcreateClasses.ConsoleLogger,ConcreateClasses" />

      

My understanding is that both the interfaces DLL and the ConcreteClasses DLL must be referenced in my project for this to work.

But what I want to do is not refer to specific implementation classes at design time. I would like them to be loaded at runtime with the path to the ConcreteClasses DLL.

Is there a way to do this?

thank

+2


source to share


2 answers


You don't need to reference a specific implementation assembly in your project, you only need to have it in the same folder as your config file, or GAC access.



It is CONVENIENT to reference another assembly with a specific implementation so that Visual Studio will place a copy of the DLL in the resulting BIN folder of your project, thus making it trivial to search.

+3


source


You can do it through reflection:



Assembly a = Assembly.LoadFrom("pathToDll");
Type interfaceType = typeof(Interfaces.ILogger);
Type implementingType = a.GetTypes.Where(t => t.IsAssignableTo(interfaceType)).First(); //add any other constraints to decide mapping

container.RegisterType(interfaceType, implementingType);

      

+1


source







All Articles