小编Mic*_*aly的帖子

Autofac - 在运行时解析IEnumerable通用接口

我发现如何使用下面的代码在运行时解析通用接口.如何IGenericInterface<>在运行时解析所有返回集合的实例.我知道在autofac我们应该使用IEnumerable<T>但我不知道如何在下面的例子中表示:

 var typeInRuntime = typeof (SubClass1);
 var instance1 = container.Resolve(typeof(IGenericInterface<>)
                          .MakeGenericType(typeInRuntime));
Run Code Online (Sandbox Code Playgroud)

这显然不起作用

 var typeInRuntime = typeof (SubClass1);
 var collection = container
                .Resolve(IEnumerable<typeof(IGenericInterface<>)
                .MakeGenericType(typeInRuntime)>);
Run Code Online (Sandbox Code Playgroud)

c# autofac

3
推荐指数
1
解决办法
1780
查看次数

标签 统计

autofac ×1

c# ×1