我正在使用反射在我的应用程序中动态生成表单。当处理具有关系的属性时,我需要能够动态调用集合。
我该如何替换:
foreach (Customer c in Db.Customers)
{
}
带有类似这样的内容:
foreach (dynamic d in Db["Customers"])
{
}
或这样
foreach (dynamic d in Db.typeof(Customer))
{
}
这项工作对您有用吗?
foreach (dynamic d in (DB.GetType().GetProperty("Customers")).GetValue(DB, null)){
}
以更易于理解的方式
var propertyInfo = DB.GetType().GetProperty("Customers");
var value = propertyInfo.GetValue(DB, null);