public static IBaseUIElement CreateInstance(Type t, By locator, IBaseElement parent, List <By> locators = null) { t = t.IsInterface ? MapInterfaceToElement.ClassFromInterface(t) : t; var constructors = t.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var con in constructors) { var conParams = con.GetParameters(); switch (conParams.Length) { case 1: { var instance = (UIElement)con.Invoke(new object[] { locator }); instance.DriverName = parent.DriverName; instance.SmartLocators = locators; instance.Parent = parent; return(instance); } case 0: { var instance = (UIElement)Activator.CreateInstance(t, true); instance.DriverName = parent.DriverName; instance.Locator = locator; instance.SmartLocators = locators; instance.Parent = parent; return(instance); } } } throw new MissingMethodException($"Can't find correct constructor to create instance of type {t}"); }
public T Get<T>(Type clazz) where T : WebBaseElement { T instance; try { instance = (T) Activator.CreateInstance(clazz.IsInterface ? MapInterfaceToElement.ClassFromInterface(clazz) : clazz); } catch { throw Exception("Can't get Cell from interface/class: " + clazz.ToString().Split("\\.").Last()); } return Get(instance); }
private static void SetRowField(TRow entity, List <FieldInfo> fields, string fieldName, ICell cell) { var field = fields.FirstOrDefault(f => NamesEqual(f.Name, fieldName)); var clazz = field?.FieldType; if (clazz == null) { return; } var value = (WebBaseElement)Activator.CreateInstance(clazz.IsInterface ? MapInterfaceToElement.ClassFromInterface(clazz) : clazz); value.WebAvatar = ((Cell)cell).WebAvatar; field.SetValue(entity, value); }
private WebBaseElement GetElementInstance(FieldInfo field, string driverName) { var type = field.GetType(); var fieldName = field.Name; var newLocator = GetNewLocator(field); try { WebBaseElement instance = null; if (type == typeof(IList)) { var elementClass = type.GetGenericArguments()[0]; /*if (elementClass.IsInterfaceOf) * elementClass = MapInterfaceToElement.ClassFromInterface(type);*/ if (elementClass != null) { instance = (WebBaseElement)Activator.CreateInstance(typeof(Elements <>).MakeGenericType(elementClass)); } } else { if (type.IsInterface) { type = MapInterfaceToElement.ClassFromInterface(type); } if (type != null) { instance = (WebBaseElement)Activator.CreateInstance(type); instance.WebAvatar.ByLocator = newLocator; } } if (instance == null) { throw Exception("Unknown interface: " + type + ". Add relation interface -> class in VIElement.InterfaceTypeMap"); } instance.Avatar.DriverName = driverName; return(instance); } catch (Exception ex) { throw Exception("Error in getElementInstance for field '%s' with type '%s'", fieldName, type.Name + ex.Message.FromNewLine()); } }
private WebBaseElement GetElementInstance(FieldInfo field, string driverName) { var type = field.FieldType; var fieldName = field.Name; var newLocator = GetNewLocator(field); return(ActionWithException(() => { WebBaseElement instance = null; if (type == typeof(List)) { throw Exception($"Can't init element {fieldName} with type 'List<>'. Please use 'IList<>' or 'Elements<>' instead"); } if (typeof(IList).IsAssignableFrom(type)) { var elementClass = type.GetGenericArguments()[0]; if (elementClass != null) { instance = (WebBaseElement)Activator.CreateInstance(typeof(Elements <>) .MakeGenericType(elementClass)); } } else { if (type.IsInterface) { type = MapInterfaceToElement.ClassFromInterface(type); } if (type != null) { instance = (WebBaseElement)Activator.CreateInstance(type); instance.WebAvatar.ByLocator = newLocator; } } if (instance == null) { throw Exception("Unknown interface: " + type + ". Add relation interface -> class in VIElement.InterfaceTypeMap"); } instance.Avatar.DriverName = driverName; return instance; }, ex => $"Error in getElementInstance for field {fieldName}' with type '{type.Name + ex.FromNewLine()}'")); }
protected override IBaseElement GetElementsRules(FieldInfo field, string driverName, Type type, string fieldName) { var newLocator = GetNewLocator(field); WebBaseElement instance = null; if (type == typeof(List <>)) { throw Exception($"Can't init element {fieldName} with type 'List<>'. Please use 'IList<>' or 'Elements<>' instead"); } if (typeof(IList).IsAssignableFrom(type)) { var elementClass = type.GetGenericArguments()[0]; if (elementClass != null) { instance = (WebBaseElement)Activator.CreateInstance(typeof(Elements <>) .MakeGenericType(elementClass)); } } else { if (type.IsInterface) { type = MapInterfaceToElement.ClassFromInterface(type); } if (type != null) { instance = (WebBaseElement)Activator.CreateInstance(type); if (newLocator != null) { instance.WebAvatar.ByLocator = newLocator; } } } if (instance == null) { throw Exception("Unknown interface: " + type + ". Add relation interface -> class in VIElement.InterfaceTypeMap"); } instance.Avatar.DriverName = driverName; return(instance); }