[按类型或字符串获取集合

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

我正在使用反射在我的应用程序中动态生成表单。当处理具有关系的属性时,我需要能够动态调用集合。

我该如何替换:

foreach (Customer c in Db.Customers)
{

}

带有类似这样的内容:

foreach (dynamic d in Db["Customers"])
{

}

或这样

foreach (dynamic d in Db.typeof(Customer))
{

}
c# reflection entity-framework-core dbcontext system.reflection
1个回答
0
投票

这项工作对您有用吗?

foreach (dynamic d in (DB.GetType().GetProperty("Customers")).GetValue(DB, null)){
}

以更易于理解的方式

var propertyInfo = DB.GetType().GetProperty("Customers");
var value = propertyInfo.GetValue(DB, null);
© www.soinside.com 2019 - 2024. All rights reserved.