Autofac:如何注册通用集合?

时间:2018-08-26 20:12:13

标签: autofac

我有几个在构造函数中使用Handler mainThreadHandler = new Handler(Looper.getMainLooper()); Runnable doToast = new Runnable() { @Override public void run() { Toast.makeText(menuActivity, "HELLO", Toast.LENGTH_SHORT).show(); } }); mainThreadHandler.post(doToast); // Queue the first execution of the code in the Runnable's run() method. mainThreadHandler.post(doToast); // Queue the second execution of the code in the Runnable's run() method. 的类。我想在Autofac中注册通用集合IList<IHero>,以便每当Autofac需要解析采用List<IHero>的服务时,它都会返回IList<IHero的新实例。下面的代码可以编译,但是在运行时会收到大量错误消息。

List<IHero>

我当前的解决方法如下:

builder.RegisterType<List<IHero>>().As<IList<IHero>>();

var printer = scope.Reseolve<IPrinter>(new TypedParameter(typeof(IList<IHero>), new List<IHero>();

1 个答案:

答案 0 :(得分:2)

不要自己注册收藏集。 Autofac handles collections for you.