You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为泛型接口IFoo<TBar>构造System.Type并通过DI解析实例

Solution for Resolving IFoo via Dependency Injection

Alright, let's break down how to implement the CreateFooUsingBarType method correctly. The core challenge here is dynamically constructing the generic IFoo<TBar> type from the incoming barType parameter, then resolving it from your DI container.

Step 1: Construct the Generic Interface Type

First, we need to get the open generic type definition for IFoo<>, then combine it with the provided barType to create the closed generic type we need.

Step 2: Resolve the Instance from DI

Once we have the closed generic type, we can use your DI scope to resolve an instance of that type, then cast it to the non-generic IFoo interface (since that's the return type of the method).

Here's the complete implementation with explanations:

public IFoo CreateFooUsingBarType(Type barType)
{
    var iocScope = GetScope();
    
    // 1. Get the open generic definition of IFoo<>
    Type openGenericFooType = typeof(IFoo<>);
    
    // 2. Create the closed generic type: IFoo<TBar> where TBar is barType
    Type closedGenericFooType = openGenericFooType.MakeGenericType(barType);
    
    // 3. Resolve the instance from the DI container
    object resolvedInstance = iocScope.ServiceProvider.GetService(closedGenericFooType);
    
    // 4. Cast to the non-generic IFoo interface (since our method returns IFoo)
    if (resolvedInstance is not IFoo fooInstance)
    {
        throw new InvalidOperationException($"No valid IFoo implementation registered for bar type {barType.FullName}");
    }
    
    return fooInstance;
}

Key Notes:

  • Open vs Closed Generic Types: typeof(IFoo<>) gives us the open generic type (without a type argument), while MakeGenericType(barType) turns it into a closed generic type specific to your barType.
  • DI Resolution: We use GetService here (you could also use GetRequiredService if you want the container to throw immediately if the type isn't registered) to fetch the instance.
  • Type Safety Check: The cast check ensures we don't return something that doesn't implement the non-generic IFoo interface, which prevents runtime type errors later.

Optional Enhancements:

  • If you want to handle cases where the type isn't registered gracefully, you can return null instead of throwing (but make sure your method's contract allows for that).
  • Add validation upfront to ensure barType is not null or an invalid type (e.g., abstract, interface if your implementations require concrete types).

内容的提问来源于stack exchange,提问作者AdamStone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:35:22