Created
April 30, 2013 09:03
-
-
Save vas6ili/5487526 to your computer and use it in GitHub Desktop.
Some useful Func<T> extensions methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace NoNamespace | |
{ | |
public static class FuncExtensions | |
{ | |
public static Func<T> ToFunc<T>(this Lazy<T> lazyValue) | |
{ | |
if (lazyValue == null) | |
throw new ArgumentNullException("lazyValue"); | |
return (Func<T>)Delegate.CreateDelegate(typeof(Func<T>), lazyValue, typeof(Lazy<T>).GetProperty("Value").GetGetMethod()); | |
} | |
public static Func<T> RunOnce<T>(this Func<T> valueFactory) | |
{ | |
if (valueFactory == null) | |
throw new ArgumentNullException("valueFactory"); | |
return new Lazy<T>(valueFactory, true).ToFunc(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment