Skip to content

Keyed Registration and Lookup

David Rickard edited this page Sep 11, 2017 · 1 revision

Types can be exported with a key of type object. You can provide it when locating to get that specific one:

container.Configure(c => c.Export<FileLogger>().AsKeyed<ILogger>("file"));
container.Configure(c => c.Export<ConsoleLogger>().AsKeyed<ILogger>("console"));

ILogger fileLogger = container.Locate<ILogger>(withKey: "file");
ILogger consoleLogger = container.Locate<ILogger>(withKey: "console");

You can also use the Key property on the [Import] attribute to pull in a specific export:

[Import(Key = "file")]
public ILogger FileLogger { protected get; set; }

Clone this wiki locally