Hmm. Talk about over-engineering. I don’t think we really need generics at all, provided we’re happy with a cast outside the method instead of inside it.
public delegate object MethodExecution();public static object GetCachedMethod(string key, DateTime absoluteExpiration, MethodExecution method){ if (HttpContext.Current.Cache[key] == null) HttpContext.Current.Cache.Insert(key, method(), null, absoluteExpiration, Cache.NoSlidingExpiration);
return HttpContext.Current.Cache[key]; }...return (DataSet)GetCachedMethod(key, DateTime.Now.AddDays(1), delegate() { return SomeMethodThatReturnsADataSet(myParam); });… Read more “Caching Method Results in ASP.NET 2.0 using Delegates”