.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、结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值