public static void ApplyAll(this IEnumerable items, string propertyName, object data) { foreach (var item in items) { PropertyInfo property = item.GetType().GetProperty(propertyName); if (property == null) { throw new NullReferenceException(); } if (!property.CanWrite) { throw new InvalidOperationException(string.Format("Property or indexer '{0}' cannot be assign to, it is read only.", property.Name)); } property.SetValue(item, TypeExtension.ChangeType(data, property.PropertyType), null); //yield return item; } }
private static T CreateItemFromRow <T>(DataRow row, IList <PropertyInfo> properties) where T : new() { T item = new T(); foreach (PropertyInfo property in properties) { if (!property.CanWrite) { continue; } if (!row.Table.Columns.Contains(property.Name)) { continue; } if (DBNull.Value != row[property.Name]) { property.SetValue(item, TypeExtension.ChangeType(row[property.Name], property.PropertyType), null); } } return(item); }