2
我想實現一個通用的方法,它允許輸出csv文件反思與仿製藥獲得的財產
public static void WriteToCsv<T>(List<T> list) where T : new()
{
const string attachment = "attachment; filename=PersonList.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
bool isFirstRow = true;
foreach (T item in list)
{
//Get public properties
PropertyInfo[] propertyInfo = item.GetType().GetProperties();
while (isFirstRow)
{
WriteColumnName(propertyInfo);
isFirstRow = false;
}
Type type = typeof (T);
StringBuilder stringBuilder = new StringBuilder();
foreach (PropertyInfo info in propertyInfo)
{
//string value ???? I am trying to get the value of the property info for the item object
}
HttpContext.Current.Response.Write(stringBuilder.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
}
HttpContext.Current.Response.End();
}
價值,但我不能獲得對象的屬性
任何建議的價值?
感謝
記住,有一些邊界情況在這裏,如索引屬性。簡單的GetValue調用會拋出一個索引器屬性,因此您必須決定是否要顯式忽略它們(可以檢查索引器參數的數目是否爲0)或以其他方式處理它們。根據您的應用程序,您可能也想要以不同的方式處理Nullable屬性。 – 2010-04-29 13:51:12