Autofac parameter passing

痞子三分冷 提交于 2019-12-08 07:53:13

问题


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

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")));

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?


回答1:


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.




回答2:


I've attached freshly built documentation for AutoFac 1.3 to AutoFac issue #121. I hope they'll resume posting official 1.3 documentation at least until they retire the 1.3 branch and, with it, support for .NET 2.0.



来源:https://stackoverflow.com/questions/471125/autofac-parameter-passing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!