读书人

请问以上VS2008代码是什么意思?帮忙详

发布时间: 2012-11-07 09:56:10 作者: rapoo

请教以下VS2008代码是什么意思?帮忙详细注释说明
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;
}

}

[解决办法]
用反射为实体类初始化,根据实体类的特性设置主键、id、表名。

读书人网 >asp.net

热点推荐