Given the code below:
private static Dictionary<Type, Action<Control>> controlDefaults = new Dictionary<Type, Action<Control>>()
{
{ typeof(TextBox), c => ((TextBox)c).Clear() }
};
How would I invoke the action in this case? This is a code snippet taken from somewhere else, and the dictionary would contain many more instances of controls. This willbe used for resetting all controls on a form to their default values.
So would I iterate as such:
foreach (Control control in this.Controls)
{
// Invoke action for each control
}
How would I then call the appropriate action from the dictionary for the current control?
Thanks.
You can write
You could also use a static generic class as the dictionary, and avoid casting:
However, you would not be able to call this in the loop, since you need to know the type at compile-time.