.NET获取实例化对象的部分属性名称
前言
项目中实例化的对象,对象中里面很有很多属性,有些是我们不需要的,有些是我们需要的,例如在下面的示例中:ID,CreateBy等属性在CB_Projects对象中是不需要的,在获取实例化对象属性名称的时候需要把这些属性过滤掉。UpdateProjectRequest是入参实例
1、定义实例化对象
using System;
using System.Collections.Generic; namespace ServiceMe.Apps.Business.Common.DAL
{ public partial class CB_Projects
{
public int ID { get; set; }
public string ProjectNumber { get; set; }
public string CimtasNumber { get; set; }
public string Root1 { get; set; }
public string Root2 { get; set; }
public string Fill1 { get; set; }
public string Fill2 { get; set; }
public string Fill3 { get; set; }
public string Fill4 { get; set; }
public string Fill5 { get; set; }
public string Fill6 { get; set; }
public Nullable<bool> IsDelete { get; set; }
public string JointNumber { get; set; }
public string OtherJoint { get; set; }
public string CreateBy { get; set; }
public Nullable<System.DateTime> CreateTime { get; set; }
public string ModifiedBy { get; set; }
public Nullable<System.DateTime> ModifiedTime { get; set; }
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace ServiceMe.Apps.Business.Common.Models.RequestModel
{
/// <summary>
/// 修改项目请求参数
/// </summary>
public class UpdateProjectRequest
{
/// <summary>
/// 主键ID
/// </summary>
public int? ID { get; set; } /// <summary>
/// 项目编号
/// </summary>
public string ProjectNumber { get; set; } /// <summary>
/// 庆达西编号
/// </summary>
public string CimtasNumber { get; set; } /// <summary>
/// root1
/// </summary>
public string Root1 { get; set; } /// <summary>
/// Root2
/// </summary>
public string Root2 { get; set; } /// <summary>
/// Fill1
/// </summary>
public string Fill1 { get; set; } /// <summary>
/// Fill2
/// </summary>
public string Fill2 { get; set; } /// <summary>
/// Fill3
/// </summary>
public string Fill3 { get; set; } /// <summary>
/// Fill4
/// </summary>
public string Fill4 { get; set; } /// <summary>
/// Fill5
/// </summary>
public string Fill5 { get; set; } /// <summary>
/// Fill6
/// </summary>
public string Fill6 { get; set; }
}
}
2、获取实例化对象的属性名称。
public static void Main(string[] args, [FromBody]UpdateProjectRequest req)
{
CB_Projects cbProjects = new CB_Projects()
{
ProjectNumber = req.ProjectNumber,
CimtasNumber = req.CimtasNumber,
Root1 = req.Root1,
Root2 = req.Root2,
Fill1 = req.Fill1,
Fill2 = req.Fill2,
Fill3 = req.Fill3,
Fill4 = req.Fill4,
Fill5 = req.Fill5,
Fill6 = req.Fill6,
ModifiedBy = req.UserAccount,
ModifiedTime = DateTime.Now
};
PropertyInfo[] propertyInfos = typeof(CB_Projects).GetProperties();
List<string> nameList = propertyInfos.Where(t => t.GetValue(cbProjects) != null && t.Name.ToUpper() != "ID").Select(t => t.Name).ToList();
string str = string.Join(",", nameList);
string[] strArray = str.Split(',');
}
3、结果。

.NET获取实例化对象的部分属性名称的更多相关文章
- c# 获取某个对象的[公有属性]的名称,类型,值
/// <summary> /// 获取某个对象的[公有属性]的名称,类型,值 /// </summary> /// <typeparam name="T&qu ...
- js遍历对象所有的属性名称和值
/* * 用来遍历指定对象所有的属性名称和值 * obj 需要遍历的对象 * author: Jet Mah * website: http://www.javatang.com/archives/2 ...
- Python+Selenium 自动化实现实例-获取测试对象的Css属性
#coding:utf-8 '''获取测试对象的css属性场景 当你的测试用例纠结细枝末节的时候,你就需要通过判断元素的css属性来验证你的操作是否达到了预期的效果.比如你可以通过判断页面上的标题字号 ...
- 获取当前对象的key的名称
获取当前对象的key的名称(无法获取),只能曲线救国 通过给标签添加class,id, 然后通过对class的遍历,来获取到id(这个id对应数据库的字段,所以对应对象的key) 然后再给 id 赋值 ...
- 遍历JavaScript某个对象所有的属性名称和值
/* * 用来遍历指定对象所有的属性名称和值 * obj 需要遍历的对象 */ function allPrpos(obj) { // 用来保存所有的属性名称和值 var props = " ...
- 对js原型对象、实例化对象及prototype属性的一些见解
什么是原型对象? 请看下面的代码,我们以各种姿势,创建了几个方法! function fn1() { } var fn2 = function () { } var fn3 = new Functio ...
- Python直接改变实例化对象的列表属性的值 导致在flask中接口多次请求报错
错误原理实例如下: class One(): list = [1, 2, 3] @classmethod def get_copy_list(cls): # copy一份list,这样对list的改变 ...
- Python脚本控制的WebDriver 常用操作 <十八> 获取测试对象的css属性
测试用例场景 当你的测试用例纠结细枝末节的时候,你就需要通过判断元素的css属性来验证你的操作是否达到了预期的效果.比如你可以通过判断页面上的标题字号以字体来验证页面的显示是否符合预期.当然,这个是强 ...
- C#获取一个实体类的属性名称、属性值
using System.Reflection; Type t = obj.GetType();//获得该类的Type foreach (PropertyInfo pi in t.GetPropert ...
随机推荐
- windows下git安装过程
参考廖雪峰博客: https://www.liaoxuefeng.com/wiki/896043488029600/896067074338496 git网站下载程序: https://git-s ...
- static、const、extern等关键字
static 参考:https://blog.csdn.net/guotianqing/article/details/79828100 http://c.biancheng.net/view/222 ...
- C++学习03_引用
基本使用 C++提供了给变量命名的机制,就是引用(Reference).引用是一种复合类型. //语法 数据类型 &name=data 注意,引用在定义时需要添加&,在使用时不能添加& ...
- BERT安装与使用
环境: python 3.5 tensorflow 1.12.1 bert-serving-server 1.9.1 bert-serving-cline 1.9.1 官网上说要保证Python &g ...
- Rust中的结构体及方法语法
这个可以和类作比较,或是go当中的方法比较. #[derive(Debug)] struct Rectangle { width: u32, height: u32, } impl Rectangle ...
- CentOS最小化安装后找不到ifconfig命令
1.ifconfig命令是设置或显示网络接口的程序,可以显示出我们机器的网卡信息, 可是有些时候最小化安装CentOS等Linux发行版的时候会默认不安装ifconfig等命令, 这时候你进入终端,运 ...
- Celery详解(2)
除了redis,还可以使用另外一个神器----Celery.Celery是一个异步任务的调度工具. Celery是Distributed Task Queue,分布式任务队列,分布式决定了可以有多个w ...
- style="position: relative;left:-5px; top: -5px;"
<span class="input-sm" style="position: relative;left:-5px; top: -5px;">&l ...
- Java+Selenium3方法篇21-webdriver处理浏览器多窗口切换
经过前面两篇文章的铺垫,我们这篇介绍,webdriver如何处理,一个浏览器上多个窗口之间切换的问题.我们先脑补这样一个测试场景,你在页面A点击一个连接,会在新的tab窗口打开页面B,这个时候,你在页 ...
- 树莓派autossh反向隧道
本来我是将树莓派连接到路由器,从而在电脑端通过IP访问.远在局域网之外的队友怎么访问呢? ssh反向隧道 它的原理比较简单: 树莓派主动向某公网服务器建立ssh连接,并请求公网服务器开启一个额外的SS ...