()=>构造

发布于 2024-09-10 19:41:22 字数 885 浏览 7 评论 0原文

我正在将一个项目从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

装迷糊 2024-09-17 19:41:32

它不是构造函数,而是 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.

凡间太子 2024-09-17 19:41:31

它是一个创建匿名委托的 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.

你又不是我 2024-09-17 19:41:30

这是一个 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) where src is the originator of the event and e 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.

回忆凄美了谁 2024-09-17 19:41:28

这是创建不带参数的内联委托的 lambda 表示法。

It's the lambda notation to create an inline delegate without parameter.

℉絮湮 2024-09-17 19:41:27

这是一个 lambda 表达式

我猜想 SetLocatorProvider 方法的签名如下:

SetLocatorProvider( Func<IServiceLocator> callback ):

现在您必须提供这样的回调。基本上有三个选项:

使用方法(始终有效):

private IServiceLocator GetServiceLocator() { /* return IServiceLocator */ }

ServiceLocator.SetLocatorProvider( GetServiceLocator() );

使用委托(需要 C#2.0): em>

ServiceLocator.SetLocatorProvider( delegate
        {
            // return IServiceLocator
        } );

使用lambda(需要 C#3.0):
这就是您看到的代码...
由于没有参数(Func 只有一个返回值),您可以使用 () 指定它:

ServiceLocator.SetLocatorProvider( () => { /* return IServiceLocator */ } );

这可以翻译为

ServiceLocator.SetLocatorProvider( () => /* IServiceLocator */ );

Maybe you Want to read 这个问题+答案

This is a lambda expression.

I guess that the SetLocatorProvider method has a signature like:

SetLocatorProvider( Func<IServiceLocator> callback ):

Now you have to provide such a callback. There are basically three options:

Use a method (always working):

private IServiceLocator GetServiceLocator() { /* return IServiceLocator */ }

ServiceLocator.SetLocatorProvider( GetServiceLocator() );

Use a delegate (requires C#2.0):

ServiceLocator.SetLocatorProvider( delegate
        {
            // return IServiceLocator
        } );

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 ():

ServiceLocator.SetLocatorProvider( () => { /* return IServiceLocator */ } );

this can be translated to

ServiceLocator.SetLocatorProvider( () => /* IServiceLocator */ );

Maybe you want to read this question + answer, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文