Example #1
0
        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;
            }
        }
Example #2
0
        private static T CreateItemFromRow <T>(DataRow row, IList <PropertyInfo> properties) where T : new()
        {
            T item = new T();

            foreach (var 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);
        }