我正在創建一個Func作爲另一個方法的參數輸入,但是如何調用該函數?作爲參數傳入時,在Func中調用方法
下面的代碼描述自己的工作:
我叫富搭配:
Foo(x => x.SlidingExpiration(TimeSpan.FromSeconds(50)));
我的foo方法:
public void Foo(Func<CacheExpiration, TimeSpan> cacheExpiration)
{
....
inside here I want to call RefreshCache, but how?.. cacheExpiration.??
}
的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;
}
}
這個問題並不完全有意義。函數'Foo'的參數是一個代理,可以根據需要將'CacheExpiration'實例變成'Timespan'。 'Foo'方法沒有引用現有的'CacheExpiration'實例傳遞給委託。你想在什麼'CacheExpiration'對象上調用'RefreshCache'?委託參數如何在這方面提供幫助?它甚至不返回一個'CacheExpiration'對象。 – Ani 2010-10-14 09:12:40