There are several options how you can implement it.
1. Factory
Create a factory for generating the instance you need:
public TestFactory : ITestFactory
{
public ITest Create(string type)
{
return new Test(type);
}
}
And register just the factory
services.AddScoped<ITestFactory, TestFactory>();
Advantage you have the single piece of code the ITestFactory which can be mocked and the whole logic how the instance and which instance is create resides there.
2. List of implementations
Register them all as ITest
services.AddScoped<ITest>(sp => new Test("ABC"));
services.AddScoped<ITest>(sp => new Test("XYZ"));
... and then use it in a class like this:
public class Consumer
{
public Consumer(IEnumerable<ITest> testInstances)
{
// This is working great if it does not matter what the instance is and you just need to use all of them.
}
}
3. ActivatorUtilities
... or just use the ActivatorUtilities which is a way to create the instance directly from a IServiceProvider see Documentation or this other SO question
var serviceProvider = <yourserviceprovider>;
var test1 = ActivatorUtilities.CreateInstance<Test>(sp, "ABC");
var test2 = ActivatorUtilities.CreateInstance<Test>(sp, "XYZ");
But like it is also written in the comment of your question it really depends what you want to do with both instances and where do you get the string type from.
You can also do more advanced ways with a combination of option 1 and 2 but therefore the better description of your problem and usage is needed.
ABCandXYZin your application and where they are coming from. Are they coming from the config file, and thus constant for the duration of the running application, and there just twoTest'registrations'? Or represent these two strings runtime data, coming from a database, or web request, and do you have otherTestinstances at runtime? Please update your post with answers to this question. Based on this information we can supply you with a good solution.