public Enum TestType { TypeA,TypeB }
1 public Interface ITest 2 {
TestType Type { get; } 3 void SayHi(); 4 }
1 public Class TestA:ITest 2 { 3 Public TestType Type => TypeA; 4 public void SayHi() 5 { 6 Console.WriteLine("TestA"); 7 } 8 } 9 10 11 public Class TestB:ITest 12 { 13 Public TestType Type => TypeB; 14 public void SayHi() 15 { 16 Console.WriteLine("TestB"); 17 } 18 }
1、program.cs中注册服务
builder.Services.AddScoped<ITest,TestA>(); builder.Services.AddScoped<ITest,TestB>();
2、新建类TestDomainService.cs,注入服务
public class TestDomainService { private readonly ITest testA; private readonly ITest testB; public TestDomainService(IEnumerable<ITest> tests) { this.testA = tests.First(t=>t.Type == TestType.TypeA); this.testB = tests.First(t=>t.Type == TestType.TypeB); } }
标签:内置,DI,TestType,TypeA,TypeB,ITest,实例,Type,public From: https://www.cnblogs.com/lixiang1998/p/16771055.html