more mucking about with hashes...
So, I got a comment on the last post about hashes from
lambdas (from Andrey
Shchekin)... It pointed out the fact that you don't need to
use expressions at all... which hilights an observation that I
hadn't made myself - such that the lambda parameter
names are available in the generated delegate... which of
course makes perfect sense!
So given:
Func
You can get the lambda parameter "Name" from the function
delegate by calling:
func.Method.GetParameters()[0].Name (would
return "Name")
Here's the revised Hash method from Andrey:
public Dictionary
Func
where T : class
{
var items = new Dictionary<>
T>();
foreach (var func in args)
{
var item =
func(null);
items.Add(func.Method.GetParameters()[0].Name, item);
}
return items;
}
very elegant and simple :)
He even did some stats, which I suspect are probably a lot more
accurate then my inital observations:
For 10000 consecutive calls:
WithAdd |
10.0144ms |
WithLambdas |
9713.968ms |
WithLambdasConstantsOnly |
240.3456ms |
WithDelegates |
30.0432ms |
of any uses I would have for it... perhaps a 2 level configuration
dictionary?
[Test]
public void HashTwoLevelDict()
{
Dictionary
object>> config = this.Hash
Assert.AreEqual(typeof(SqlClientDriver),
config["Connection"]["DriverClass"]);
}
abused for non-functional programming tasks :)