提问人:Phil 提问时间:4/4/2023 最后编辑:CharliefacePhil 更新时间:4/4/2023 访问量:94
C# 从数组字符串生成匿名类型
C# Build anonymous type from array string
问:
我正在寻找一种方法来从对象构建匿名类型,其中它的属性名称与给定字符串数组中的属性名称匹配。像这样:
//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 */ });
关于如何实现这一目标的任何想法?
答: 暂无答案
上一个:访问泛型类中的属性
评论
Dictionary<string, object>
object
ExpandoObject
Type.GetProperties()
PropertyInfo.GetValue()/SetValue()
foreach