Last active
September 22, 2015 16:12
-
-
Save jbrestan/be72abc6abf8bd5f1337 to your computer and use it in GitHub Desktop.
Inspired by this article, except for C#: http://benjiweber.co.uk/blog/2015/08/17/lambda-parameter-names-with-reflection/
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
public IDictionary<string, TValue> Dict<TValue>( | |
params Func<dynamic, TValue>[] pairs) | |
{ | |
return pairs.ToDictionary(pair => pair.Method.GetParameters()[0].Name, | |
pair => pair(null)); | |
} |
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
void Main() | |
{ | |
// Classic way | |
var d2 = new Dictionary<string, string> | |
{ | |
{"foo", "bar"}, | |
{"omg", "1"} | |
}; | |
// "Literal" | |
var d = Dict( | |
foo => "bar", | |
omg => 1.ToString() | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment