Hi I have an interface which is implemented by multiple classes.
public interface IHuman
{
void Talk();
}
public class AdultHuman
{
public void Talk()
{
Console.Writeline("Hi");
}
}
public class BabyHuman
{
public void Talk()
{
Console.Writeline("Babble");
}
}
public enum HumanEnums
{
Adult,
Baby
}
Currently in my startup add on I have
services.AddSingleton<AdultHuman>();
services.AddSingleton<BabyHuman>();
We are constantly adding different implementations of IHumans so I would like my start up add on to be dynamic to add the singletons with a forloop looping through the values of the HumanEnums so it would look like this
var enumTypes = Enum.GetValues(typeof(ActionTypes));
foreach(var enum in enumTypes)
{
var type = typeof(IHuman);
// namespace + name of class i.e MyProgram.Humans.BabyHuman
var typeName = $"{type.Namespace}.{action}Human";
var t = Type.GetType(typeName, true);
services.AddSingleton< --something here-- >();
}
How would I achieve this?
P.S. Also it would be helpful if instead of looping through the enums, I could find all implementations of IHuman and loop through that.