C# 无法访问常规列表项的属性
您好,访问已向函数传递参数的泛型列表的属性时出错。我的代码如下: 节目 类用户 类链式系统 类CSV 我得到以下错误: 错误1“T”不包含“UserID”的定义,并且找不到接受类型为“T”的第一个参数的扩展>方法“UserID”>是否缺少using指令或程序集引用?>D:\Development\OOP\CSV.cs 14 39 OOP 我假设错误存在于Write函数中,但我不知道问题出在哪里。Write是一种通用方法。按原样编写,您可以使用任何类型T调用它。当然,您不能假设任何类型都具有属性UserId。改为这样做:C# 无法访问常规列表项的属性,c#,.net,C#,.net,您好,访问已向函数传递参数的泛型列表的属性时出错。我的代码如下: 节目 类用户 类链式系统 类CSV 我得到以下错误: 错误1“T”不包含“UserID”的定义,并且找不到接受类型为“T”的第一个参数的扩展>方法“UserID”>是否缺少using指令或程序集引用?>D:\Development\OOP\CSV.cs 14 39 OOP 我假设错误存在于Write函数中,但我不知道问题出在哪里。Write是一种通用方法。按原样编写,您可以使用任何类型T调用它。当然,您不能假设任何类型都具有属性U
public static void Write(List<User> list)
{
Console.WriteLine(list.Count);
Console.WriteLine(list[0].UserID);
}
Write是一种通用方法。按原样编写,您可以使用任何类型T调用它。当然,您不能假设任何类型都具有属性UserId。改为这样做:
public static void Write(List<User> list)
{
Console.WriteLine(list.Count);
Console.WriteLine(list[0].UserID);
}
由于将方法设置为泛型,因此出现此错误
public static void Write(List<User> list)
{
Console.WriteLine(list.Count);
Console.WriteLine(list[0].UserID);
}
由于将方法设置为泛型,因此出现此错误
public static void Write(List<User> list)
{
Console.WriteLine(list.Count);
Console.WriteLine(list[0].UserID);
}
要访问T的属性,我们需要使用反射,因为我们不知道T的详细信息,即列表
public void WriteData<T>(ref ExcelWorksheet workSheet,List<T> list)
{
workSheet.TabColor = System.Drawing.Color.Black;
workSheet.DefaultRowHeight = 12;
//Header of table
//
workSheet.Row(1).Height = 20;
workSheet.Row(1).Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
workSheet.Row(1).Style.Font.Bold = true;
...
...
...
foreach(T item in list)
{
System.Reflection.PropertyInfo[] propertyInfo = item.GetType().GetProperties();
int cellIndex = 1;
workSheet.Column(cellIndex).AutoFit();
foreach (System.Reflection.PropertyInfo info in propertyInfo)
{
object value = info.GetValue(item, null);
workSheet.Cells[recordIndex, cellIndex++].Value = value;
}
recordIndex++;
}
}
要访问T的属性,我们需要使用反射,因为我们不知道T的详细信息,即列表
public void WriteData<T>(ref ExcelWorksheet workSheet,List<T> list)
{
workSheet.TabColor = System.Drawing.Color.Black;
workSheet.DefaultRowHeight = 12;
//Header of table
//
workSheet.Row(1).Height = 20;
workSheet.Row(1).Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
workSheet.Row(1).Style.Font.Bold = true;
...
...
...
foreach(T item in list)
{
System.Reflection.PropertyInfo[] propertyInfo = item.GetType().GetProperties();
int cellIndex = 1;
workSheet.Column(cellIndex).AutoFit();
foreach (System.Reflection.PropertyInfo info in propertyInfo)
{
object value = info.GetValue(item, null);
workSheet.Cells[recordIndex, cellIndex++].Value = value;
}
recordIndex++;
}
}
public void WriteData<T>(ref ExcelWorksheet workSheet,List<T> list)
{
workSheet.TabColor = System.Drawing.Color.Black;
workSheet.DefaultRowHeight = 12;
//Header of table
//
workSheet.Row(1).Height = 20;
workSheet.Row(1).Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
workSheet.Row(1).Style.Font.Bold = true;
...
...
...
foreach(T item in list)
{
System.Reflection.PropertyInfo[] propertyInfo = item.GetType().GetProperties();
int cellIndex = 1;
workSheet.Column(cellIndex).AutoFit();
foreach (System.Reflection.PropertyInfo info in propertyInfo)
{
object value = info.GetValue(item, null);
workSheet.Cells[recordIndex, cellIndex++].Value = value;
}
recordIndex++;
}
}