属性控制类
属性控制类
[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"";
}
}
[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"";
}
}
千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs