标签:方法 模型 summary where ESS 继承 typeof ati efi
1.添加基类BaseMappingName,继承Attribute
public class BaseMappingName: Attribute
{
public string _mappingName = null;
/// <summary>
/// 初始化类变量
/// </summary>
/// <param name="mappingName"></param>
public BaseMappingName(string mappingName)
{
this._mappingName = mappingName;
}
/// <summary>
/// 获取名称
/// </summary>
/// <returns></returns>
public string GetMappingName()
{
return this._mappingName;
}
}
2.添加表名类(约束为类)继承BaseMappingName
[AttributeUsage(AttributeTargets.Class)]//约束:适用于类
public class TableAttribute: BaseMappingName
{
/// <summary>
/// 类的初始化
/// </summary>
/// <param name="tableName"></param>
public TableAttribute(string tableName):
base(tableName)
{
}
}
3.添加列名(约束为类的属性)继承BaseMappingName
[AttributeUsage(AttributeTargets.Property)]//约束为类的属性
public class ColumnAttribute: BaseMappingName
{
/// <summary>
/// 类的初始化
/// </summary>
/// <param name="columnName"></param>
public ColumnAttribute(string columnName) :
base(columnName)
{
}
//public string GetMappingName()
//{
// return this._ColoumnName;
//}
}
4.类名获取类
public static class DBMappingExtend
{
public static string GetMappingName<T>(this T t) where T : MemberInfo//this 扩展方法
{
if (t.IsDefined(typeof(BaseMappingName), true))
{
var attribute =
t.GetCustomAttribute<BaseMappingName>();
return attribute.GetMappingName();
}
else
{
return t.Name;
}
}
}
5.使用方法
类的使用
[Table("Company")]
public class Company: BaseModel
{
[Column("Name1")]
public string Name { get; set; }
}
类的属性获取
Type type = typeof(Class.Company);
var gg =string.Join(",",type.GetProperties().Select(p=>p.GetMappingName()));
MessageBox.Show(gg);
标签:方法 模型 summary where ESS 继承 typeof ati efi
原文地址:https://www.cnblogs.com/lanseqingchen/p/12497468.html