/// <summary> /// Copies all primitive values from one object to another. /// </summary> /// <param name="From">Object to take values from.</param> /// <param name="To">Object to copy values to.</param> public static void CopyTo(this object From, object To, string[] SkipProperties = null) { if (From == null) { throw new ArgumentNullException("From"); } if (To == null) { throw new ArgumentNullException("To"); } if (SkipProperties == null) { SkipProperties = new string[] { }; } foreach (PropertyInfo piOld in From.GetType().GetProperties()) { if (!piOld.IsPrimitiveType()) { continue; } if (SkipProperties.Contains(piOld.Name)) { continue; } PropertyInfo piNew = To.GetType().GetProperty(piOld.Name); if (piNew == null) { continue; } if (!piNew.CanWrite) { continue; } piNew.SetValue(To, piOld.GetValue(From, null), null); } }
private IList <Props> GetProps() { var type = ControlInfo.GetType(); lock (props) { if (props.ContainsKey(type)) { return(props[type]); } var properties = type.GetProperties(); var list = new List <Props>(); if (!SkipAutoDetect) { foreach (var property in properties) { if (SkipProperties.Contains(property.Name)) { continue; } if (property.Name.Equals("nameRuControl") && typeof(TextBox).IsAssignableFrom(property.PropertyType)) { foreach (var stringProp in (new[] { "nameRuRodControl", "nameRuDatControl", "nameRuTvorControl", "nameRuVinControl", "nameRuToControl", "nameRuComeControl", "nameRuPredControl" })) { var propertyTo = type.GetProperty(stringProp); if (propertyTo != null) { list.Add(new Props { ChangeValueIfNull = true, From = property, To = propertyTo }); } } } else if (property.Name.Equals("nameKzControl") && typeof(TextBox).IsAssignableFrom(property.PropertyType)) { foreach (var stringProp in (new[] { "nameKzRodControl", "nameKzDatControl", "nameKzTvorControl", "nameKzVinControl", "nameKzToControl", "nameKzComeControl", "nameKzPredControl", "nameKzIshControl" })) { var propertyTo = type.GetProperty(stringProp); if (propertyTo != null) { list.Add(new Props { ChangeValueIfNull = true, From = property, To = propertyTo }); } } } if (property.Name.StartsWith("nameRu") && property.Name.EndsWith("Control") && typeof(TextBox).IsAssignableFrom(property.PropertyType)) { var propertyTo = type.GetProperty("nameKz" + property.Name.Substring(6)); if (propertyTo != null) { list.Add(new Props { ChangeValueIfNull = true, From = property, To = propertyTo }); } } else if (property.Name.EndsWith("RuControl") && typeof(TextBox).IsAssignableFrom(property.PropertyType)) { var propertyTo = type.GetProperty(property.Name.Substring(0, property.Name.Length - 9) + "KzControl"); if (propertyTo != null) { list.Add(new Props { ChangeValueIfNull = true, From = property, To = propertyTo }); } } } } foreach (var property in AdditionalProperties) { if (property.From == null && property.FromProperty != null) { property.From = type.GetProperty(property.FromProperty); } if (property.To == null && property.ToProperty != null) { property.To = type.GetProperty(property.ToProperty); } if (property.From != null && property.To != null) { list.Add(property); } } //list.AddRange(AdditionalProperties); props[type] = list; return(list); } }