public Button GetButton(string buttonName) { List <FieldInfo> fields = _element.GetFields(typeof(IButton)); if (fields.Count == 1) { return((Button)fields[0].GetValue(_element)); } var buttons = fields.Select(f => (Button)f.GetValue(_element)); var button = buttons.First(b => NamesEqual(ToButton(b.Name), ToButton(buttonName))); if (button == null) { throw Exception($"Can't find button '{buttonName}' for Element '{ToString()}'"); } return(button); }
public Button GetButton(string buttonName) { var fields = _element.GetFields(typeof(IButton)); switch (fields.Count) { case 0: throw Exception($"Can't find ny buttons on form {ToString()}'"); case 1: return((Button)fields[0].GetValue(_element)); default: var buttons = fields.Select(f => (Button)f.GetValue(_element)).ToList(); var button = buttons.FirstOrDefault(b => NamesEqual(ToButton(b.Name), ToButton(buttonName))); if (button == null) { throw Exception($"Can't find button '{buttonName}' for Element '{ToString()}'." + $"(Found following buttons: {buttons.Select(el => el.Name).Print()}).".FromNewLine()); } return(button); } }