57

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

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

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.

I'd appreciate your help. Thanks

nfplee
  • 7,643
  • 12
  • 63
  • 124
  • 1
    I think you should also post the declaration (delegate) of ExposeConfiguration, + what is it that you want to save in _configuration (declaration would also help). – Manish Basantani Dec 07 '10 at 16:51

3 Answers3

92

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.

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
  • 1
    +1, but note a lambda expression can map to any compatible delegate type. It's just often used with `Func<>` – JaredPar Dec 07 '10 at 16:39
  • 1
    Is it not the other way around? Func rather than Func, ... etc.? – NeverStopLearning Jan 29 '14 at 09:17
  • 1
    @NeverStopLearning Yes, you're right. I got the type parameters the wrong way around. I've corrected my answer. – Colin Mackay Jan 29 '14 at 14:31
  • Why am I getting a `cannot convert from 'System.Func' to 'System.Linq.Expressions>'`? Maybe it's one of the exceptions where it doesn't map - I'm trying to set a binding in Xamarin.Forms. – jbyrd Apr 07 '16 at 18:47
  • 3
    Great answer, although I was a little confused at first how T1 and TResult correspond with the function. Here's an example that may be a bit clearer: `Func numberToString = (num) => { return num + "";};` – frodo2975 Jul 19 '16 at 21:35
1

With C# 7.0 local functions

TResult matchDuplicates (T1 c) => _configuration = c;
Alexandre Daubricourt
  • 3,323
  • 1
  • 34
  • 33
0

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>();
Wayne Koorts
  • 10,861
  • 13
  • 46
  • 72
Brian Ball
  • 12,268
  • 3
  • 40
  • 51