[英]access property of type parameter object
I am using the below code 我正在使用以下代码
public static void SaveArrayAsCSV<T>(T[] arrayToSave, string fileName)
{
using (StreamWriter file = new StreamWriter(fileName))
{
foreach (T item in arrayToSave)
{
//item.GetType().GetField("AccountName")
//item.GetType().GetProperty("AccountName")
file.WriteLine(item + ",");
}
}
}
Here the item has property called AccountName
, 此处,该项目具有名为
AccountName
属性,
I tried to access it using 我试图使用访问它
item.GetType().GetField("AccountName") //returns null
item.GetType().GetProperty("AccountName") //returns an object, not able to spot the actual value in it
But both are not returning the values. 但是两者都没有返回值。 But when I check using the quick watch I am able to see the property
但是当我使用快速手表检查时,我可以看到物业
您需要使用GetValue
方法,请尝试以下操作:
var accountName = item.GetType().GetProperty("AccountName").GetValue(item);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.