In that case, simple use of the Open Generics capabilities of TinyIoC might be enough.
public class Consumer
{
public Consumer(ILogger<Consumer> logger) {}
}
public interface ILogger<T>
{
string Log(string message);
}
public class ConcreteLogger<T>:ILogger<T>
{
public string Log(string message)
{
return string.Format("{0}: {1}", typeof (T), message);
}
}
and then do
container.Register(typeof (ILogger<>), typeof (ConcreteLogger<>));
That will cause an instance of ConcreteLogger<Consumer>
to be injected into the Consumer
constructor.
Edit
If you need to have an ILogger
reference, you can simply make ILogger<T>
a "marker interface" only and have it "derive" from ILogger
:
public interface ILogger<T> : ILogger
{
}
The Consumer
class would still need to take a dependency on ILogger<Consumer>
, but ConcreteLogger<T>
would directly implement ILogger
members.
I see no better way, especially if the ILogger
interface is fixed because it is not your own. I don't think there is a way to ask TinyIoC for the type it is currently trying to resolve to then be able to act on that, so if you need type specific implementations, the types must be made known by other means - and Generics are often (not always) helpful with that.