【问题标题】:GET Collection By Type or String [duplicate]按类型或字符串获取集合[重复]
【发布时间】:2020-09-30 05:50:10
【问题描述】:

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

如何替换:

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】:

这对你有用吗?

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

以更容易理解的方式

var propertyInfo = DB.GetType().GetProperty("Customers");
var value = propertyInfo.GetValue(DB, null);

【讨论】:

    猜你喜欢
    • 2019-11-09
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 2011-01-25
    • 2012-04-09
    • 2019-04-30
    相关资源
    最近更新 更多