Skip to content

Instantly share code, notes, and snippets.

@vas6ili
Created April 30, 2013 09:03
Show Gist options
  • Save vas6ili/5487526 to your computer and use it in GitHub Desktop.
Save vas6ili/5487526 to your computer and use it in GitHub Desktop.
Some useful Func<T> extensions methods
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