C# 从数组字符串构建匿名类型

问题描述 投票:0回答:0

我正在寻找一种从属性名称与给定字符串数组中的属性名称匹配的对象构建匿名类型的方法。像这样的东西:

//Current Way:
    var anonObject = primaryObject.Select(m=> new {m.property1, m.property2, m.property3, m.property4, ...etc});
//Ideal Way:
    var propertyArray= new string["property1","property3"];
    var anonObject = primaryObject.Select(m=> new { /* Something here where we step through primaryObject.GetProperties() and only choose the ones where the name is contained in the above array */ });

关于如何实现这一点有什么想法吗?

c# reflection automapper system.reflection anonymous-types
© www.soinside.com 2019 - 2024. All rights reserved.