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

ftlcloud(超云)9元/月,1G内存/1核/20g硬盘/10M带宽不限/10G防御,美国云服务器

ftlcloud怎么样?ftlcloud(超云)目前正在搞暑假促销,美国圣何塞数据中心的云服务器低至9元/月,系统盘与数据盘分离,支持Windows和Linux,免费防御CC攻击,自带10Gbps的DDoS防御。FTL-超云服务器的主要特色:稳定、安全、弹性、高性能的云端计算服务,快速部署,并且可根据业务需要扩展计算能力,按需付费,节约成本,提高资源的有效利用率。点击进入:ftlcloud官方网站...

妮妮云36元,美国VPS洛杉矶 8核 8G 36元/月,香港葵湾 8核 8G

妮妮云的来历妮妮云是 789 陈总 张总 三方共同投资建立的网站 本着“良心 便宜 稳定”的初衷 为小白用户避免被坑妮妮云的市场定位妮妮云主要代理市场稳定速度的云服务器产品,避免新手购买云服务器的时候众多商家不知道如何选择,妮妮云就帮你选择好了产品,无需承担购买风险,不用担心出现被跑路 被诈骗的情况。妮妮云的售后保证妮妮云退款 通过于合作商的友好协商,云服务器提供2天内全额退款,超过2天不退款 物...

hosthatch:14个数据中心15美元/年

hosthatch在做美国独立日促销,可能你会说这操作是不是晚了一个月?对,为了准备资源等,他们拖延到现在才有空,这次是针对自己全球14个数据中心的VPS。提前示警:各个数据中心的网络没有一个是针对中国直连的,都会绕道而且ping值比较高,想买的考虑清楚再说!官方网站:https://hosthatch.com所有VPS都基于KVM虚拟,支持PayPal在内的多种付款方式!芝加哥(大硬盘)VPS5...

propertyinfo为你推荐
jmhjmh是谁jsmJSM们有用过什么化妆品能使鼻子和脸部立体泛珍珠白的感觉的产品吗?心水分享,谢谢oa办公系统下载完全免费的oa办公系统腾讯公告腾讯出什么事了 所有QQ用户空间农场什么的都打不开 有的连空间都进不去 又受攻击了?人肉搜索引擎怎样使用人肉搜索引擎?xcelsius谁有水晶易表2008的密钥?移动硬盘文件或目录损坏且无法读取移动硬盘提示文件或目录损坏且无法读取,怎么修复中信银行理财宝中信银行理财宝金卡怎样激活第三方支付系统→第三方支付平台有哪些病毒分析网站电脑中病毒了,应该去哪个网站?
中文域名注册 如何注册网站域名 512au 申请个人网页 浙江独立 韩国名字大全 刀片服务器的优势 湖南idc 个人免费邮箱 免费个人网页 大化网 学生机 hosting 认证机构 卡巴斯基免费版 ddos攻击 最年轻博士 免费空间申请 电脑主机声音很大 防盗报警主机 更多