() => construct

2019-06-25 08:25发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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.



回答5:

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.