Autofac参数传递 [英] Autofac parameter passing

查看:90
本文介绍了Autofac参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试集成最新版本的autofac(1.3.3.54),并且遇到了以下问题.

I've been trying to integrate the latest version of autofac (1.3.3.54), and I'm running into the following problem.

根据该项目的Google代码Wiki,建议在Register回调中使用参数的推荐方式如下:

The recommended way of consuming parameters in the Register callback, per the Google code wiki for the project is as follows:

builder.Register(((c,p)=> new Foo(p.Get("arg1"))));

builder.Register((c, p) => new Foo(p.Get("arg1")));

但是,这不能与上述版本的autofac代码一起编译.我查看了源代码,发现p是IEnumerable(ComponentActivatorWithParameters).有关文档的代码是否过时?

However this won't compile with the mentioned version of the autofac code. I looked through the source and I see that p is an IEnumerable (ComponentActivatorWithParameters). Is the code out of date with respect to the documentation?

推荐答案

似乎代码已更改,Wiki上的文档尚未更新.现在,获取"方法为命名",参数"类为"NamedParameter".请参见以下示例:

It appears that the code has changed and the documentation on the Wiki has not been updated. The "Get" method is now "Named" and the "Parameter" class is now "NamedParameter". See the following example:

var builder = new ContainerBuilder();
builder.Register((c, p) => new Person(p.Named<string>("name")));

using (var container = builder.Build())
{
    var person = container.Resolve<Person>(new NamedParameter("name", "Fred"));    
}

希望有人可以很快更新文档.

Hopefully someone can update the documentation soon.

这篇关于Autofac参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆