小子pk了

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

 

 

 

posted on 2019-07-11 15:28  小子pk了  阅读(711)  评论(0编辑  收藏  举报

导航