propertyinfoEntity Framework框架这段代码什么意思

propertyinfo  时间:2021-06-08  阅读:()

C#中,一个类里有三个属性,如何获得其中特定的一个属性的名字呢?

对于你的要求:属性名字是不固定的,所以我们只能根据属性其他方面的特征来得到这个属性的名字,比如访问修饰符 属性类型,而方法修饰符在这个类中已经重复了,所以只能根据属性类型。

所以我们首先先得到所有的属性,然后遍历该属性集合,如果属性为Int32类型 就说明该属性是我们要的属性。

然后得到该属性的名字赋给str。

代码示例:

public void MyMethod() { string str; //首先得到该类的所有属性 System.Reflection.PropertyInfo[] ps = this.GetType().GetProperties(); //遍历该属性集合 foreach (System.Reflection.PropertyInfo property in ps) { //如果类型为Int32 就是该属性 if (property.PropertyType.ToString()=="System.Int32") { //得到该属性的名字 赋给变量 str = property.Name; } } }

C#中使用propertyGridView如何实现如下效果就是如果有字段为空值则不对其进行显示

public class Test { [CategoryAttribute("C1"), DisplayName("属性1"), Browsable(true)] public string T1 { get; set; } [CategoryAttribute("C1"), DisplayName("属性2"), Browsable(true)] public string T2 { get; set; } [CategoryAttribute("C2"), DisplayName("属性3"), Browsable(true)] public string T3 { get; set; } [CategoryAttribute("C2"), DisplayName("属性4"), Browsable(true)] public string T4 { get; set; } [CategoryAttribute("C3"), DisplayName("属性5"), Browsable(true)] public string T5 { get; set; } [CategoryAttribute("C3"), DisplayName("属性6"), Browsable(true)] public string T6 { get; set; } [CategoryAttribute("C4"), DisplayName("属性7"), Browsable(true)] public string T7 { get; set; } } Test t = new Test(); // t.T3 = "123"; t.T5 = "321312"; t.T7 = "13213"; Type type = typeof(System.ComponentModel.CategoryAttribute); PropertyDescriptorCollection props = TypeDescriptor.GetProperties(t); PropertyInfo[] pArr = typeof(Test).GetProperties(); Dictionary<string, bool> a = new Dictionary<string, bool>(); Dictionary<string, List<PropertyInfo>> b = new Dictionary<string, List<PropertyInfo>>(); foreach (var property in pArr) { AttributeCollection attrs = props[property.Name].Attributes; FieldInfo fld = type.GetField("categoryValue", BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); string key = fld.GetValue(attrs[type]).ToString(); if (b.ContainsKey(key)) { b[key].Add(property); } else { b.Add(key, new List<PropertyInfo>() { property }); } if (a.ContainsKey(key)) { if (!a[key]) { object value = property.GetValue(t, null); a[key] = value != null; } } else { object value = property.GetValue(t, null); a.Add(key, value != null); } } Type browsableType = typeof(System.ComponentModel.BrowsableAttribute); foreach (var item in b) { if (a[item.Key]) { continue; } foreach (var prop in item.Value) { AttributeCollection attrs = props[prop.Name].Attributes; FieldInfo fld = browsableType.GetField("browsable", BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); fld.SetValue(attrs[browsableType], false); } } propertyGrid1.SelectedObject = t;

Entity Framework框架这段代码什么意思

Entity Framework 对象删除的时候 是直接提交更改 包括增删改 上面代码中的增删改 是把对象做相应的标记EntityState.Added 相当于待添加 这样并不是马上就添加 而是提交SaveChanges()时做相应的增删改 LoadEntities 这个的话 就是一个自定义Where查询

湖北22元/月(昔日数据)云服务器,国内湖北十堰云服务器,首月6折

昔日数据怎么样?昔日数据新上了湖北十堰云服务器,湖北十堰市IDC数据中心 母鸡采用e5 2651v2 SSD MLC企业硬盘 rdid5阵列为数据护航 100G高防 超出防御峰值空路由2小时 不限制流量。目前,国内湖北十堰云服务器,首月6折火热销售限量30台价格低至22元/月。(注意:之前有个xrhost.cn也叫昔日数据,已经打不开了,一看网站LOGO和名称为同一家,有一定风险,所以尽量不要选择...

Vultr VPS新增第18个数据中心 瑞典斯德哥尔摩欧洲VPS主机机房

前几天还在和做外贸业务的网友聊着有哪些欧洲机房的云服务器、VPS商家值得选择的。其中介绍他选择的还是我们熟悉的Vultr VPS服务商,拥有比较多达到17个数据中心,这不今天在登录VULTR商家的时候看到消息又新增一个新的机房。这算是第18个数据中心,也是欧洲VPS主机,地区是瑞典斯德哥尔摩。如果我们有需要欧洲机房的朋友现在就可以看到开通的机房中有可以选择瑞典机房。目前欧洲已经有五个机房可以选择,...

牦牛云(3.5USD/月 )阿里云国际版云服务器 1核1G40G

收到好多消息,让我聊一下阿里云国际版本,作为一个阿里云死忠粉,之前用的服务器都是阿里云国内版的VPS主机,对于现在火热的阿里云国际版,这段时间了解了下,觉得还是有很多部分可以聊的,毕竟,实名制的服务器规则导致国际版无需实名这一特点被无限放大。以前也写过几篇综合性的阿里云国际版vps的分析,其中有一点得到很多人的认同,那句是阿里云不管国内版还是国际版的IO读写速度实在不敢恭维,相对意义上的,如果在这...

propertyinfo为你推荐
excel计算公式求excel计算公式大全iso20000认证ISO20000认证应用雷达雷达有什么用途视频压缩算法1分钟视频的大小在10M左右的格式有哪些?12种颜色12种颜色都是代表什么防火墙排名什么防火墙世界第一啊?(急!!!)js后退在全局js中屏蔽了后退功能,但是想让自己定义的后退有用assemblyinfoasp.net这几个文件是干什么的?部署工具win10 评估和部署工具包有什么用棋牌论坛三个知名的游戏相关论坛,要求有网址的!
虚拟主机99idc 双线虚拟主机 域名买卖 免费vps vps代购 lnmp 腾讯云盘 buyvm wavecom directadmin 360抢票助手 密码泄露 七夕促销 韩国名字大全 老左正传 网通服务器托管 qq金券 阿里云邮箱登陆地址 网页加速 网络速度 更多