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

 

 

 

转载于:https://www.cnblogs.com/vuenote/p/11170294.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Core 中,可以使用以下代码将 `appsettings.json` 文件中的配置信息实例化为一个对象: 首先,创建一个类来存储需要实例化的配置信息。例如,如果 `appsettings.json` 文件中有以下配置信息: ```json { "MyConfig": { "MyValue1": "Value1", "MyValue2": "Value2" } } ``` 则可以创建以下类来存储这些配置信息: ```csharp public class MyConfig { public string MyValue1 { get; set; } public string MyValue2 { get; set; } } ``` 然后,在 `Program.cs` 文件中设置配置文件路径和名称: ```csharp public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) => { config.SetBasePath(Directory.GetCurrentDirectory()); config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); }) .UseStartup<Startup>(); ``` 最后,在需要实例化配置信息的地方,注入 `IConfiguration` 接口,并使用 `GetSection` 方法获取配置信息,然后使用 `Get<T>` 方法将配置信息实例化为一个对象: ```csharp public class MyClass { private readonly IConfiguration _config; private readonly MyConfig _myConfig; public MyClass(IConfiguration config) { _config = config; _myConfig = _config.GetSection("MyConfig").Get<MyConfig>(); } public void MyMethod() { var value1 = _myConfig.MyValue1; var value2 = _myConfig.MyValue2; } } ``` 在上面的示例中,`MyClass` 类通过构造函数注入 `IConfiguration` 接口,然后使用 `GetSection` 方法获取 `appsettings.json` 文件中 `MyConfig` 节点下的配置信息,并使用 `Get<T>` 方法将其实例化为一个 `MyConfig` 对象。然后,可以通过访问该对象的属性获取配置信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值