c#中获取不到字段上的attribute

王尔玉 2018-01-12 09:27:59
[AttributeUsage(AttributeTargets.Field)]
public class UColumn : Attribute
{
public DBField type;

public UColumn(DBField type)
{
this.type = type;
}
}
[AttributeUsage(AttributeTargets.Class)]
public class UTable : Attribute
{
public string tableName;
public UTable(string tableName)
{
tableName = tableName ?? throw new ArgumentNullException(nameof(tableName));
}

}

先定义了俩attribute,一个作用在类上,一个作用在字段上,然后
[UTable("T_Content")]
public class T_Content
{
[UColumn(DBField.None)]
private string contentID;
private string content_AlarmID;
private string message;
private string user;
private DateTime? dateTime = null;
public string ContentID { get => contentID; set => contentID = value; }
public string Content_AlarmID { get => content_AlarmID; set => content_AlarmID = value; }
public string Message { get => message; set => message = value; }
public string User { get => user; set => user = value; }
public DateTime? DateTime { get => dateTime; set => dateTime = value; }


}

实体类上加attribute
然后测试 发现可以获取到UTable但是获取不到UColumn
然后是测试代码:
...全文
483 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ourhouzi 2018-01-15
  • 打赏
  • 举报
回复
获取字段特性是 t_Content.GetType().GetFields();
  • 打赏
  • 举报
回复
你的自定义Attribute类名不对,没有以Attribute结尾

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧