Created
January 1, 2020 16:57
-
-
Save greglecki/cb56cc7237014165c211943ea5fdba56 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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