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

Hostodo独立日提供四款特价年付VPS套餐 最低年付$13.99

前天,还有在"Hostodo商家提供两款大流量美国VPS主机 可选拉斯维加斯和迈阿密"文章中提到有提供两款流量较大的套餐,这里今天看到有发布四款庆祝独立日的七月份的活动,最低年付VPS主机13.99美元,如果有需要年付便宜VPS主机的可以选择商家。目前,Hostodo机房可选拉斯维加斯和迈阿密两个数据中心,且都是基于KVM虚拟+NVMe整列,年付送DirectAdmin授权,需要发工单申请。(如何...

PIGYUN:美国联通CUVIPCUVIP限时cuvip、AS9929、GIA/韩国CN2机房限时六折

pigyun怎么样?PIGYunData成立于2019年,2021是PIGYun为用户提供稳定服务的第三年,目前商家提供香港CN2线路、韩国cn2线路、美西CUVIP-9929、GIA等线路优质VPS,基于KVM虚拟架构,商家采用魔方云平台,所有的配置都可以弹性选择,目前商家推出了七月优惠,韩国和美国所有线路都有相应的促销,六折至八折,性价比不错。点击进入:PIGYun官方网站地址PIGYUN优惠...

Megalayer(48元)新增 美国CN2优化线路特价服务器和VPS方案

Megalayer 商家算是新晋的服务商,商家才开始的时候主要是以香港、美国独立服务器。后来有新增菲律宾机房,包括有VPS云服务器、独立服务器、站群服务器等产品。线路上有CN2优化带宽、全向带宽和国际带宽,这里有看到商家的特价方案有增加至9个,之前是四个的。在这篇文章中,我来整理看看。第一、香港服务器系列这里香港服务器会根据带宽的不同区别。我这里将香港机房的都整理到一个系列里。核心内存硬盘IP带宽...

propertyinfo为你推荐
视频技术短视频运营每天的工作是做什么?需要所学的技术都有什么方面?vga接口定义VGA接口通常用来连接哪些设备,各个脚代表什么意思,它的连线是如何焊接的?腾讯贴吧QQ应该有贴吧的吧?那怎样进QQ贴吧?在(=@__@=)哪里?收费视频微信里的视频通话是怎么收费 ?人脸识别解锁华为手机人脸识别解锁如何设置电子邮件软件电子邮件软件哪个好用上传图片网站速度超快的上传图片网站什么是网络地址手机IP地址是什么?视频服务器软件服务器方面软件那个网站最多,急啊!!!!如果带教程的更好,嘿嘿……鸿道集团不是说收回了商标权了吗?为什么红罐王老吉还有“王老吉”三个字?红罐不是属于鸿道公司的么?
免费国际域名 什么是二级域名 香港vps99idc 云图标 牛人与腾讯客服对话 godaddy域名证书 web服务器架设 共享主机 河南移动网 免费高速空间 免费申请网站 怎么建立邮箱 新睿云 彩虹云 google台湾 web服务器是什么 免费外链相册 lamp什么意思 如何登陆阿里云邮箱 ssl加速 更多