Storing a Lambda Expression in a Variable

前端 未结 3 1796
野性不改
野性不改 2020-12-30 18:39

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 t

相关标签:
3条回答
  • 2020-12-30 19:08

    Sure. Assuming _configuration is going to store what you were using in your first bit of code. It should look something like this:

    return Fluently.Configure().ExposeConfiguration(c => {
    c.EventListeners.PostInsertEventListeners = _configuration;
    c.EventListeners.PostUpdateEventListeners = _configuration;});
    

    If there are any kind of cast errors from the compiler, you can always use:

    _configuration.Cast<IPostInsertEventListeners>();
    
    0 讨论(0)
  • 2020-12-30 19:13

    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;
    

    Replacing TResult and T1 with the relevant types.

    That might work for you.

    0 讨论(0)
  • 2020-12-30 19:14

    With C# 7.0 local functions

    TResult matchDuplicates (T1 c) => _configuration = c;
    
    0 讨论(0)
提交回复
热议问题