Skip to content

Instantly share code, notes, and snippets.

@greglecki
Created January 1, 2020 16:57
Show Gist options
  • Select an option

  • Save greglecki/cb56cc7237014165c211943ea5fdba56 to your computer and use it in GitHub Desktop.

Select an option

Save greglecki/cb56cc7237014165c211943ea5fdba56 to your computer and use it in GitHub Desktop.
private Setting GetSetting<T>(Expression<Func<T>> expr)
{
var me = expr.Body as MemberExpression;
if (me == null)
throw new ArgumentException("Invalid expression. It should be MemberExpression");
var func = expr.Compile(); //This converts our expression back to a Func
var value = func(); //Run the func to get the setting value
return new Setting(me.Member.Name,value);
}
// Usage
var settings = new List<Setting>();
settings.Add(GetSetting(() => Settings.EnableBugs));
settings.Add(GetSetting(() => Settings.EnableFluxCapacitor));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment