public class ColumnMapAttribute : Attribute
{
private readonly string _name;
public ColumnMapAttribute(string name)
{
_name = name;
}
public string Name { get { return _name; } }
}
public class DbModelBase
{
public DbModelBase()
{
}
public DbModelBase(DataRow row)
{
foreach (var tuple in GetType().GetProperties().Select(p =>
{
object[] objs = p.GetCustomAttributes(typeof(ColumnMapAttribute), false);
return new Tuple<PropertyInfo, string>(p, objs.Length > 0 ? ((ColumnMapAttribute)objs[0]).Name : null);
}).Where(t => t.Item2 != null))
{
object value = row[tuple.Item2];
if (value != DBNull.Value)
{
Type type = tuple.Item1.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
//DateTime? -> DateTime
type = type.GetGenericArguments()[0];
}
object changedValue = Convert.ChangeType(value, type);
tuple.Item1.SetValue(this, changedValue, null);
}
}
}
}
public class ColumnMapAttribute : Attri