属性控制类

属性控制类
[AttributeUsage(AttributeTargets.Class, Inherited =false)]
[ComVisible(
true)]
publicclass ClassKeyAttribute : Attribute
{
public ClassKeyAttribute()
{

}
public ClassKeyAttribute(string TableName)
{
this.TableName = TableName;
}
publicstring TableName { get; set; }

publicstaticstring GetTableName<T>()
{
T model
= (T)Activator.CreateInstance(typeof(T));
return GetTableName(model);
}

publicstaticstring GetTableName(object model)
{
object[] attributes = model.GetType().GetCustomAttributes(false);
foreach (object o in attributes)
{
if (o is ClassKeyAttribute)
{
ClassKeyAttribute attr
= (ClassKeyAttribute)o;
return attr.TableName;
}
}
return"";
}
}

 

posted @ 2010-04-02 20:55  kingkoo  阅读(340)  评论(0编辑  收藏  举报