(类似于 this question )
我有名为“问题”和“FinalFigures”的表格:
问题(问题 ID、文本、FinalFiguresColumnName)
FinalFigures(FinalFigureID、TotalDays、TotalCost 等)
'FinalFiguresColumnName' 将具有“TotalDays”、“TotalCost”等值。是否有一种简单的方法可以遍历一组问题并将“Text”值保存到“FinalFigures”上的相应列名称表?
即。而不是:
var item = new FinalFigure();
item.TotalDays = "x";
我需要:
var item = new FinalFigure();
// access the column 'TotalDays' with its name as a string, not a property
最佳答案
您可以使用一些反射来通过属性的字符串名称获取属性值,而不是直接访问它。
var value = (int)item.GetType().GetProperty("TotalDays").GetValue(item);
要设置值,只需调用 SetValue();
item.GetType().GetProperty("TotalDays").SetValue(item, value);
关于c# - LINQ - 以列名作为字符串参数访问列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686139/