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查询

美国G口/香港CTG/美国T级超防云/物理机/CDN大促销 1核 1G 24元/月

[六一云迎国庆]转盘活动实物礼品美国G口/香港CTG/美国T级超防云/物理机/CDN大促销六一云 成立于2018年,归属于西安六一网络科技有限公司,是一家国内正规持有IDC ISP CDN IRCS电信经营许可证书的老牌商家。大陆持证公司受大陆各部门监管不好用支持退款退现,再也不怕被割韭菜了!主要业务有:国内高防云,美国高防云,美国cera大带宽,香港CTG,香港沙田CN2,海外站群服务,物理机,...

台湾云服务器整理推荐UCloud/易探云!

台湾云服务器去哪里买?国内有没有哪里的台湾云服务器这块做的比较好的?有很多用户想用台湾云服务器,那么判断哪家台湾云服务器好,不是按照最便宜或最贵的选择,而是根据您的实际使用目的选择服务器,只有最适合您的才是最好的。总体而言,台湾云服务器的稳定性确实要好于大陆。今天,云服务器网(yuntue.com)小编来介绍一下台湾云服务器哪里买和一年需要多少钱!一、UCloud台湾云服务器UCloud上市云商,...

HostYun全场9折,韩国VPS月付13.5元起,日本东京IIJ线路月付22.5元起

HostYun是一家成立于2008年的VPS主机品牌,原主机分享组织(hostshare.cn),商家以提供低端廉价VPS产品而广为人知,是小成本投入学习练手首选,主要提供基于XEN和KVM架构VPS主机,数据中心包括中国香港、日本、德国、韩国和美国的多个地区,大部分机房为国内直连或者CN2等优质线路。本月商家全场9折优惠码仍然有效,以KVM架构产品为例,优惠后韩国VPS月付13.5元起,日本东京...

propertyinfo为你推荐
waze去国外旅行,哪个APP比较实用决策树分析如何用SPSS生成决策树并对新数据进行预测分析y码女款衣服XXL、XL、XXXL尺码分别是多大?qq博客怎么开QQ博客啊!搜索引擎的概念7 什么是搜索引擎?如何在Internet上搜索图片和文字资料的?jstz江苏泰州市地税如何申报?数据挖掘项目将来想从事统计分析和数据挖掘,需要哪些技能asp大马问:ASP是什么?ASP根据什么制作木马的?ASP木马和大马有什么区别?云计划云计划创富平台怎么样?有谁知道。介绍一下。谷歌图片识别如何打开PICASA头像识别功能?
域名备案中心 ftp空间 inmotionhosting idc评测网 新世界电讯 华为云主机 铁通流量查询 linux空间 北京双线机房 91vps 服务器是干什么的 linux服务器维护 免费智能解析 爱奇艺会员免费试用 吉林铁通 我的世界服务器ip 贵阳电信 qq金券 tracker服务器 学生机 更多