public abstract class BaseDAL<TEntity> where TEntity : new()
{
public BaseDAL()
{
Type t = typeof(TEntity);
List<string> primaryKeys = new List<string>();
PropertyInfo[] properties = t.GetProperties();
foreach (PropertyInfo p in properties)
{
if (p.GetCustomAttributes(typeof(PrimaryKeyAttribute), false).Length > 0)
{
primaryKeys.Add(p.Name);
}
if (p.GetCustomAttributes(typeof(IdentityFieldAttribute), false).Length > 0)
{
IdentityField = p.Name;
}
}
PrimaryKey = primaryKeys.ToArray();
Object[] tableNameAttributes = t.GetCustomAttributes(typeof(TableNameAttribute), false);
if (tableNameAttributes.Length > 0)
{
TableName = ((TableNameAttribute)tableNameAttributes[0]).TableName;
}
else
{
TableName = t.Name;
} }
{
public BaseDAL()
{
Type t = typeof(TEntity);
List<string> primaryKeys = new List<string>();
PropertyInfo[] properties = t.GetProperties();
foreach (PropertyInfo p in properties)
{
if (p.GetCustomAttributes(typeof(PrimaryKeyAttribute), false).Length > 0)
{
primaryKeys.Add(p.Name);
}
if (p.GetCustomAttributes(typeof(IdentityFieldAttribute), false).Length > 0)
{
IdentityField = p.Name;
}
}
PrimaryKey = primaryKeys.ToArray();
Object[] tableNameAttributes = t.GetCustomAttributes(typeof(TableNameAttribute), false);
if (tableNameAttributes.Length > 0)
{
TableName = ((TableNameAttribute)tableNameAttributes[0]).TableName;
}
else
{
TableName = t.Name;
} }
public abstract class BaseDAL<TEntity> where TEntity : new()
这个定义了一个抽象泛型类BaseDAL,包含一个泛型类型TEntity,这个类型必须有一个无参数构造函数。List<string> primaryKeys = new List<string>();
这是创建一个泛型列表对象。其余调用函数都是反射相关的,不懂的话,把光标放在不懂的函数上,按f1看msdn帮助。