LiXiang98

导航

 

 

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);
    } 
}

 

posted on 2022-10-09 09:36  LiXiang98  阅读(22)  评论(0)    收藏  举报