views:

32

answers:

2

Hello.

I was creating a Func as parameter input to another method, but how do I invoke that?

The following code describes my work:

I call Foo with:

Foo(x => x.SlidingExpiration(TimeSpan.FromSeconds(50)));

My method Foo:

public void Foo(Func<CacheExpiration, TimeSpan> cacheExpiration)
{
....
inside here I want to call RefreshCache, but how?.. cacheExpiration.??

}

The CacheExpiration :)

public class CacheExpiration
        {
            TimeSpan timeSpan;
            bool sliding;

            public TimeSpan SlidingExpiration(TimeSpan ts)
            {
                this.timeSpan = ts;
                this.sliding = true;
                return ts;
            }
            public TimeSpan AbsoluteExpiration(TimeSpan ts)
            {
                this.timeSpan = ts;
                return ts;
            }

            public bool RefreshCache(MetaObject mo)
            {
                //some logic....
                return true;
            }

        }
+1  A: 
var ts = cacheExpiration(yourCacheExpiration);
leppie
A: 

If I'm reading this correctly then the Func cacheExpiration takes a CacheExpiration instance and returns a TimeSpan. So I could see the body of Foo being:

TimeSpan ts = cacheExpiration.SlidingExpiration(TimeSpan.FromSeconds(50));
//or
TimeSpan ts2 = cacheExpiration.AbsoluteExpiration(TimeSpan.FromSeconds(50));

This doesn't line up with the lamda in your example so I'm guessing from that you really want cacheExpiration to be a Func that takes a Timespan and returns a TimeSpan. But this wouldn't work for the RefreshCache method as it takes a MetaObject and returns a boolean.

Daniel Ballinger
NServiceBus can be configured like:NServiceBus.Configure.With().Log4Net<YourAppender>(a => a.YourProperty = "value");why I cant do something similar? Foo(x => x.SlidingExpiration(TimeSpan.FromSeconds(50)));
Janus007
In visual studio you could get the signature of the .Log4Net<AppenderSkeleton> method from NServiceBus by going to its definition. It looks like they are passing a function to configure the generic type.
Daniel Ballinger