() => construction

I am going to convert a project from visual studio 2005 to visual studio 2008 and come to the above design.

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

May I understand what it is.

+4
source share
5 answers

This is a lambda expression.

I assume that the SetLocatorProvider method has such a signature as:

 SetLocatorProvider( Func<IServiceLocator> callback ): 

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

Use the method (always running):

 private IServiceLocator GetServiceLocator() { /* return IServiceLocator */ } ServiceLocator.SetLocatorProvider( GetServiceLocator() ); 

Use delegate (C # 2.0 required):

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

Use lambda (C # 3.0 required):
This is the code you see ...
Since there is no argument ( Func<IServiceLocator> has only a return value), you indicate this with () :

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

it can be translated into

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

Perhaps you want to read this question + answer .

+10
source

This is the lambda designation for creating an inline delegate without a parameter.

+2
source

This is lambda. If you are familiar with delegates, this is like declaring a method that returns an injector and using it as a delegate, except that you entered a method.

The first () contains lambda arguments. For example, when processing events, you often see (src, e) , where src is the initiator of the event, and e is the event. The arguments are then available for later use by the code.

If it's multi-line, you can put (args) => { brackets } around the delegate and return the value. This is a reduction.

+2
source

This is a lambda expression that an anonymous delegate creates. That is, its function is declared built-in. The list of parameters is inside the parenthesis, so in this case there are no parameters. And when a function contains one operator, it implicitly returns the value of this operator (or returns nothing at all).

In this particular case, it is a function that returns an injector. This is a common pattern for ServiceLocator to initialize with a function that returns an IoC container.

0
source

This is not a constructor, but an expression of Lambda. See here for more details.

In this case () means that no parameters => are Lamda operators and the injector is returned.

0
source

Source: https://habr.com/ru/post/1315933/


All Articles