User.CS[TableAttribute("User")]
public class User:SuperEntity
{
[Colum("userID", DbType = DbType.Int32)]
public int UserID { get; set; } [Colum("UserName", DbType = DbType.String)]
public string UserName { get; set; } public void Save()
{
}
}SuperEntity.CSpublic abstract class SuperEntity
{
private int aaa;
public bool InsertEntity(object obj)
{
string strSql = ORMHelper.GetInsertSqlStr(obj);
if (SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, strSql) > 0)
{
return true;
}
else
{
return false;
}
} public T GetInfo<T>(int pkid)
{
T local = Activator.CreateInstance<T>();
Type type = local.GetType();
TableAttribute temp = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), false).First();
string tabName = temp.TableName;
SqlDataReader dr = SqlHelper.ExecuteReader(System.Data.CommandType.Text,"Select * from ["+tabName+"] where UserID="+pkid);
while(dr.Read())
{
for(int i=0;i<dr.FieldCount;i++)
{
PropertyInfo proper = type.GetProperty(dr.GetName(i));
proper.SetValue(dr.GetName(i),dr[i], null);
}
}
//PropertyInfo[] Propertys = type.GetProperties();
//foreach (var item in Propertys)
//{ item.SetValue(
// object[] attributes = item.GetCustomAttributes(false);
// foreach (var item1 in attributes)
// {
// //取列名
// ColumAttribute colum = item1 as ColumAttribute;
// if (colum != null)
// {
// //columValue.Add(colum.ColumName, value);
// }
// }
//}
// ColumAttribute colTemp = (ColumAttribute)type.GetCustomAttributes(typeof(ColumAttribute), false).First();
return local;
}
}TableAttribute.CS[Serializable, AttributeUsage(AttributeTargets.Class)]
public class TableAttribute : Attribute
{
//保存表名的字段
private string _tableName;
public TableAttribute() { }
public TableAttribute(string tableName)
{
this._tableName = tableName;
}
// 映射的表名(表的全名:模式名.表名)///
public string TableName
{
set { this._tableName = value; }
get { return this._tableName; }
}
}
请问我在SuperEntity这个类里的这个方法中
public T GetInfo<T>(int pkid)
{
T local = Activator.CreateInstance<T>();
//为什么我访问不到local.TableAttribute.TableName,运行时时T为User类请高手帮我分析一下?怎么样去设置T的属性,到最后返回出去谢谢了
public class User:SuperEntity
{
[Colum("userID", DbType = DbType.Int32)]
public int UserID { get; set; } [Colum("UserName", DbType = DbType.String)]
public string UserName { get; set; } public void Save()
{
}
}SuperEntity.CSpublic abstract class SuperEntity
{
private int aaa;
public bool InsertEntity(object obj)
{
string strSql = ORMHelper.GetInsertSqlStr(obj);
if (SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, strSql) > 0)
{
return true;
}
else
{
return false;
}
} public T GetInfo<T>(int pkid)
{
T local = Activator.CreateInstance<T>();
Type type = local.GetType();
TableAttribute temp = (TableAttribute)type.GetCustomAttributes(typeof(TableAttribute), false).First();
string tabName = temp.TableName;
SqlDataReader dr = SqlHelper.ExecuteReader(System.Data.CommandType.Text,"Select * from ["+tabName+"] where UserID="+pkid);
while(dr.Read())
{
for(int i=0;i<dr.FieldCount;i++)
{
PropertyInfo proper = type.GetProperty(dr.GetName(i));
proper.SetValue(dr.GetName(i),dr[i], null);
}
}
//PropertyInfo[] Propertys = type.GetProperties();
//foreach (var item in Propertys)
//{ item.SetValue(
// object[] attributes = item.GetCustomAttributes(false);
// foreach (var item1 in attributes)
// {
// //取列名
// ColumAttribute colum = item1 as ColumAttribute;
// if (colum != null)
// {
// //columValue.Add(colum.ColumName, value);
// }
// }
//}
// ColumAttribute colTemp = (ColumAttribute)type.GetCustomAttributes(typeof(ColumAttribute), false).First();
return local;
}
}TableAttribute.CS[Serializable, AttributeUsage(AttributeTargets.Class)]
public class TableAttribute : Attribute
{
//保存表名的字段
private string _tableName;
public TableAttribute() { }
public TableAttribute(string tableName)
{
this._tableName = tableName;
}
// 映射的表名(表的全名:模式名.表名)///
public string TableName
{
set { this._tableName = value; }
get { return this._tableName; }
}
}
请问我在SuperEntity这个类里的这个方法中
public T GetInfo<T>(int pkid)
{
T local = Activator.CreateInstance<T>();
//为什么我访问不到local.TableAttribute.TableName,运行时时T为User类请高手帮我分析一下?怎么样去设置T的属性,到最后返回出去谢谢了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货