非非以非非 發表於 2025-3-26 16:36:00

依赖注入接口多实现如何获取指定服务

<p>原为链接&nbsp;https://www.cnblogs.com/ysmc/p/18794061</p>
<p>  在上一个文章中,我们讲过&nbsp;键控服务 服务,可惜的是这个需要 .NET 8 才能使用,那我们在 .NET 8 之前应该怎么找到我们需要的服务呢,本文给大家讲讲使用特性的方式</p>
<p>  本人依旧秉承着短小精悍,废话不多,直接上代码!</p>
<p>首先,我们写一个特性,自定义特性需要继承 Attribute,并且我们给一个只能 get 的 public 的属性,用于后续获取指定服务使用</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ServerKeyAttribute : Attribute
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> Key { <span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)">; }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> ServerKeyAttribute(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> key)
    {
      Key </span>=<span style="color: rgba(0, 0, 0, 1)"> key;
    }
}</span></pre>
</div>
<p>随便定义一个接口</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> ITestService
{
    Task GetValue();
}</span></pre>
</div>
<p>然后就是多写几个实现,并且使用上面的自定义特性</p>
<div class="cnblogs_code">
<pre>
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Test1Service : ITestService
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> GetValue()
    {
      Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Test1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    }
}


</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Test2Service : ITestService
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> GetValue()
    {
      Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Test2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    }
}


</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Test3Service : ITestService
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> GetValue()
    {
      Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Test3</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    }
}</span></pre>
</div>
<p>注册服务</p>
<div class="cnblogs_code">
<pre>builder.Services.AddTransient&lt;ITestService, Test1Service&gt;<span style="color: rgba(0, 0, 0, 1)">();
builder.Services.AddTransient</span>&lt;ITestService, Test2Service&gt;<span style="color: rgba(0, 0, 0, 1)">();
builder.Services.AddTransient</span>&lt;ITestService, Test3Service&gt;();</pre>
</div>
<p>最后我们简单点,在&nbsp;Controller 中获取指定服务,因为我比较懒</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span>
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">async</span> Task GetServers( IEnumerable&lt;ITestService&gt;<span style="color: rgba(0, 0, 0, 1)"> testServices)
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>   <span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> service <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> testServices)
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">    {
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>         <span style="color: rgba(0, 0, 255, 1)">var</span> attributes = service.GetType().GetCustomAttributes(<span style="color: rgba(0, 0, 255, 1)">typeof</span>(ServerKeyAttribute), <span style="color: rgba(0, 0, 255, 1)">false</span>) <span style="color: rgba(0, 0, 255, 1)">as</span> IEnumerable&lt;ServerKeyAttribute&gt;<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (attributes != <span style="color: rgba(0, 0, 255, 1)">null</span> &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> attributes.Any())
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>             <span style="color: rgba(0, 0, 255, 1)">if</span> (attributes.Any(x =&gt; x.Key == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Test2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)">            {
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">                service.GetValue();
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">15</span>
<span style="color: rgba(0, 128, 128, 1)">16</span>   <span style="color: rgba(0, 0, 255, 1)">await</span><span style="color: rgba(0, 0, 0, 1)"> Task.CompletedTask;
</span><span style="color: rgba(0, 128, 128, 1)">17</span> }</pre>
</div>
<p>结果</p>
<p><img src="https://img2024.cnblogs.com/blog/1897432/202503/1897432-20250326163051681-711000913.png" alt="" loading="lazy"></p>
<p>好嘞,完事,是不是非常简单,非常感谢各位大佬的观看</p>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:一事冇诚,转载请注明原文链接:https://www.cnblogs.com/ysmc/p/18794061</p><br><br>
来源:https://www.cnblogs.com/ysmc/p/18794061
頁: [1]
查看完整版本: 依赖注入接口多实现如何获取指定服务