将Lambda表达式存储在变量中 [英] Storing a Lambda Expression in a Variable

查看:84
本文介绍了将Lambda表达式存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我在努力做一些简单的事情时已经变得不高兴了.在我的应用程序中,我具有以下代码来配置Nhibernate(我的问题并不特定于Nhibernate).

I think my brain has become fried as i'm struggling to do something simple. In my application i have the following code to configure Nhibernate (my issue is not specific to Nhibernate).

return Fluently.Configure()
    .ExposeConfiguration(c => {
        c.EventListeners.PostInsertEventListeners = new IPostInsertEventListener[] { new LoggingEventListener() };
        c.EventListeners.PostUpdateEventListeners = new IPostUpdateEventListener[] { new LoggingEventListener() };
   });

但是,我需要将配置(ExposeConfiguration中的内容)存储在私有变量中.我可以执行以下操作:

However I need to store the configuration (the stuff inside ExposeConfiguration) inside a private variable. I can do the following:

return Fluently.Configure()
    .ExposeConfiguration(c => _configuration = c);

其中_configuration是私有变量.但这并没有增加我的额外配置选项(EventListeners的东西).我玩过各种各样的东西,但我想我对lambda的了解并不如我所想.

Where _configuration is a private variable. But this doesn't add my extra configuration options (the EventListeners stuff). I've played around with various things but i guess my lambda knowledge isn't as good as i thought.

感谢您的帮助.谢谢

推荐答案

lambda表达式只是通常映射到 Func< T1,T2,...,TResult> 之一的委托.变体.

A lambda expression is just a delegate that often maps to one of the Func<T1, T2, ..., TResult> variants.

Func<T1, TResult> myVar = c => _configuration = c;

用相关类型替换 TResult T1 .

这可能对您有用.

这篇关于将Lambda表达式存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆