Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反射和泛型获得属性的值_C#_.net_Asp.net_Generics_Reflection - Fatal编程技术网

C# 反射和泛型获得属性的值

C# 反射和泛型获得属性的值,c#,.net,asp.net,generics,reflection,C#,.net,Asp.net,Generics,Reflection,我试图实现一个通用的方法,允许输出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

我试图实现一个通用的方法,允许输出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();
    }
publicstaticvoidwritetocsv(列表列表),其中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(“内容处置”,附件);
HttpContext.Current.Response.ContentType=“text/csv”;
HttpContext.Current.Response.AddHeader(“Pragma”、“public”);
bool isFirstRow=true;
foreach(列表中的T项)
{                
//获取公共属性
PropertyInfo[]PropertyInfo=item.GetType().GetProperties();
while(第一行)
{
WriteColumnName(propertyInfo);
isFirstRow=false;
}
类型=类型(T);
StringBuilder StringBuilder=新的StringBuilder();
foreach(PropertyInfo中的PropertyInfo信息)
{
//字符串值???我正在尝试获取item对象的属性信息的值
}                
HttpContext.Current.Response.Write(stringBuilder.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
}
HttpContext.Current.Response.End();
}
但我无法获得对象属性的值

有什么建议吗

谢谢你这样说:

object value = info.GetValue(item, null);
给你

PropertyInfo[] propertyInfo = item.GetType().GetProperties(); 
var val = propertyInfo.GetValue(item, null);

请记住,这里有一些边界情况,例如索引器属性。简单的GetValue调用将抛出索引器属性,因此您必须决定是显式忽略它们(可以检查索引器参数的数量是否为0)还是以其他方式处理它们。根据应用程序的不同,您可能还希望以不同的方式处理可为null的属性。