So a normal Dictionary<string, Action>
I use like so:
validationsDictionary["ThisValidation"]();
However, strings can be missed typed. So I would like to use the properties of a model as the key:
validationsDictionary[x => x.ThisProperty]();
But, I don't know exactly what the type is going to be, I have tried these:
Dictionary<Func<Model>, Action>
Dictionary<Expressions<Model>, Action>
Dictionary<Expression<Func<Model>>, Action>
I know some people don't rate using functions as keys. So I could do something like this:
void Validate(Expression<Func<Model>> key)
{
validationsDictionary[key.ToString()]();
}
I don't know if key.ToString()
is a correct property to use, but you get the gist.
EDIT
So, I used this:
Expression<Func<DisplayContentViewModel, object>> predicate
And it works like a treat giving me the ability to do: x => x.SomeProperty
I figured I can use predicate.Name
to give a string representation of the name. So now all I have to figure out, is how to populate the dictionary!