()=>构造
我正在将一个项目从 Visual Studio 2005 转换为 Visual Studio 2008,并提出了上述结构。
using Castle.Core.Resource;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using CommonServiceLocator.WindsorAdapter;
using Microsoft.Practices.ServiceLocation;
namespace MyClass.Business
{
public class Global : System.Web.HttpApplication
{
public override void Init()
{
IServiceLocator injector =
new WindsorServiceLocator(
new WindsorContainer(
new XmlInterpreter(
new ConfigResource("oauth.net.components"))));
//ServiceLocator.SetLocatorProvider(() => injector);
// ServiceLocator.SetLocatorProvider(injector);
}
}
}
ServiceLocator.SetLocatorProvider(() => 注入器);
我可以了解一下这是什么吗?
I am in the process of converting a project from visual studio 2005 to visual studio 2008 and came up on the above construct.
using Castle.Core.Resource;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using CommonServiceLocator.WindsorAdapter;
using Microsoft.Practices.ServiceLocation;
namespace MyClass.Business
{
public class Global : System.Web.HttpApplication
{
public override void Init()
{
IServiceLocator injector =
new WindsorServiceLocator(
new WindsorContainer(
new XmlInterpreter(
new ConfigResource("oauth.net.components"))));
//ServiceLocator.SetLocatorProvider(() => injector);
// ServiceLocator.SetLocatorProvider(injector);
}
}
}
ServiceLocator.SetLocatorProvider(() => injector);
Can I get an understanding of what this is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它不是构造函数,而是 Lambda 表达式。有关详细信息,请参阅此处。
在这种情况下()意味着没有参数=>是 lamda 操作员,并且正在返回注射器。
It is not a constructor but a Lambda expression. See here for more detail.
In this case () means no parameters => is the Lamda operator and injector is being returned.
它是一个创建匿名委托的 lambda 表达式。也就是说,它是一个声明为内联的函数。参数列表位于括号内,因此在本例中没有参数。当函数包含单个语句时,它会隐式返回该语句的值(或根本不返回任何内容)。
在这种特定情况下,它是一个返回注入器的函数。这是 ServiceLocator 的常见模式,使用返回 IoC 容器的函数对其进行初始化。
It is a lambda expression that creates an anonymous delegate. That is, its a function declared inline. The parameter list is inside the paranthesis, so in this case there are no parameters. And when the function contains a single statement it implicitly returns the value of that statement (or returns nothing at all).
In this specific case, it is a function that returns the injector. This is a common pattern for a ServiceLocator, to initialize it with a function that returns the IoC container.
这是一个 lambda。如果您熟悉委托,那么这就像声明一个返回
injector
的方法并将其用作委托,只不过您已经内联了该方法。第一个
()
包含 lambda 的参数。例如,在事件处理中,您经常会看到(src, e)
,其中src
是事件的发起者,e
是事件的发起者。事件本身。然后,这些参数可供后续代码使用。如果是多行,你可以把
(args) =>; {括号}
将委托括起来并返回值。这是简写。It's a lambda. If you're familiar with delegates, it's like declaring a method which returns
injector
and using that as a delegate, except you've inlined the method.The first
()
contains the arguments to the lambda. For instance, in event handling, you'll often see(src, e)
wheresrc
is the originator of the event ande
is the event itself. The arguments are then available for the subsequent code to use.If it's multiline, you can put
(args) => { brackets }
around the delegate and return the value. This is shorthand.这是创建不带参数的内联委托的 lambda 表示法。
It's the lambda notation to create an inline delegate without parameter.
这是一个 lambda 表达式。
我猜想
SetLocatorProvider
方法的签名如下:现在您必须提供这样的回调。基本上有三个选项:
使用方法(始终有效):
使用委托(需要 C#2.0): em>
使用lambda(需要 C#3.0):
这就是您看到的代码...
由于没有参数(
Func
只有一个返回值),您可以使用()
指定它:这可以翻译为
Maybe you Want to read 这个问题+答案。
This is a lambda expression.
I guess that the
SetLocatorProvider
method has a signature like:Now you have to provide such a callback. There are basically three options:
Use a method (always working):
Use a delegate (requires C#2.0):
Use a lambda (requires C#3.0):
That's the code you see ...
Since there is no argument (
Func<IServiceLocator>
has only a return value) you specify this by using()
:this can be translated to
Maybe you want to read this question + answer, too.