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

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

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

CloudServer:$4/月KVM-2GB/50GB/5TB/三个数据中心

CloudServer是一家新的VPS主机商,成立了差不多9个月吧,提供基于KVM架构的VPS主机,支持Linux或者Windows操作系统,数据中心在美国纽约、洛杉矶和芝加哥机房,都是ColoCrossing的机器。目前商家在LEB提供了几款特价套餐,最低月付4美元(或者$23.88/年),购买更高级别套餐还能三个月费用使用6个月,等于前半年五折了。下面列出几款特别套餐配置信息。CPU:1cor...

VoLLcloud:超便宜香港CMI大带宽vps-三网CMI直连-年付四免服务-低至4刀/月-奈飞

vollcloud LLC创立于2020年,是一家以互联网基础业务服务为主的 技术型企业,运营全球数据中心业务。致力于全球服务器租用、托管及云计算、DDOS安 全防护、数据实时存储、 高防服务器加速、域名、智能高防服务器、网络安全服务解决方案等领域的智 能化、规范化的体验服务。所有购买年付产品免费更换香港原生IP(支持解锁奈飞),商家承诺,支持3天内无条件退款(原路退回)!点击进入:vollclo...

propertyinfo为你推荐
决策树分析事件树分析法的介绍知识分享平台知识付费平台有哪些?数据监测什么是媒体监测?awvAWV的转换器 要免费的 看好是AWV不是AMV印度尼西亚国家代码国际代码移动硬盘文件或目录损坏且无法读取移动硬盘文件或目录损坏且无法读取怎么办??人脸识别解锁华为手机人脸识别解锁如何设置微粒群算法粒子群算法优化下面的公式 要怎么做 能给个程序最好免费下载空间要下载免费空间播放器怎么弄embed函数c语言“sqrt函数”的用法是什么?
国外网站空间 国外服务器租用 vps虚拟服务器 域名交易网 香港机房 bash漏洞 日志分析软件 godaddy优惠券 域名转向 腾讯实名认证中心 免费高速空间 cdn加速是什么 1美金 四核服务器 789电视剧 架设邮件服务器 下载速度测试 德隆中文网 游戏服务器出租 申请免费空间 更多