gpt4 book ai didi

visual-studio-2008 - () => 构造

转载 作者:行者123 更新时间:2023-12-04 22:08:33 28 4
gpt4 key购买 nike

我正在将一个项目从 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(() => 注入(inject)器);

我能理解这是什么吗。

最佳答案

这是 lambda expression .

我猜 SetLocatorProvider方法的签名如下:

SetLocatorProvider( Func<IServiceLocator> callback ):

现在你必须提供这样的回调。基本上有三种选择:

使用 方法 (总是工作):
private IServiceLocator GetServiceLocator() { /* return IServiceLocator */ }

ServiceLocator.SetLocatorProvider( GetServiceLocator() );

使用 代表 (需要 C#2.0):
ServiceLocator.SetLocatorProvider( delegate
{
// return IServiceLocator
} );

使用 λ (需要 C#3.0):
就是你看到的代码...
由于没有参数( Func<IServiceLocator> 只有一个返回值),您可以使用 () 来指定它。 :
ServiceLocator.SetLocatorProvider( () => { /* return IServiceLocator */ } );

这可以翻译成
ServiceLocator.SetLocatorProvider( () => /* IServiceLocator */ );

也许你想阅读 this question + answer , 也。

关于visual-studio-2008 - () => 构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3265776/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com