using System;
using System.Reflection;
namespace CustomAttributes
{
class Program
{
static void Main(string[] args)
{
//1.获取类的特性
Type type = typeof(T);
//1.1判断是否存在特性
if (type.IsDefined(typeof(ColumnAttribute)))
{
//1.2获取特性的值
TableAttribute attr = type.GetCustomAttribute<ColumnAttribute>();
Console.WriteLine(attr.Name);
}
//2.获取类属性的特性
PropertyInfo[] properties = type.GetProperties();
foreach (var item in properties)
{
var attr= item.GetCustomAttribute<ColumnAttribute>();
if (attr!= null)
{
Console.WriteLine(item.Name);
Console.WriteLine(attr.Name);
}
}
}
}
}
04-05
1764