300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > C#获取实体类字段信息PropertyInfo 字段名称 字段值 字段属性标签

C#获取实体类字段信息PropertyInfo 字段名称 字段值 字段属性标签

时间:2021-01-25 23:50:30

相关推荐

C#获取实体类字段信息PropertyInfo 字段名称 字段值 字段属性标签

可用于EF获取实体字段是否为主键key

引用空间:

System.Reflection.PropertyInfo

AnUser anUser = new AnUser();anUser.Id = "514f275979f64531b7fbbb2f89c8af49";anUser.UserNo = "5566110";PropertyInfo[] props = typeof(AnUser).GetProperties();//实体的字段列表foreach (var item in props){//item.Name 获取字段名称if (item.Name == "Id"){string id = item.GetValue(anUser) as string;//获取字段值}var kkkk = item.Attributes;var kkkk2 = item.CustomAttributes;//自定义的属性标签//获取字段是否有[Key]属性标签bool isHave = kkkk2.Any(x => x.AttributeType == typeof(ponentModel.DataAnnotations.KeyAttribute));}

using System;using System.Collections.Generic;using ponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Ayy.Models{/// <summary>/// 用户/// </summary>[Table("AnUser")]public class AnUser{/// <summary>/// guid主键/// </summary>[Key]public string Id {set; get; }/// <summary>/// 工号/// </summary>public string UserNo {get; set; }}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。