如何创建一个接受所有 类 类型的通用列表参数?

How to create a Generic list parameter which will accept all the type of classes?

public void GenerateExcelReport(List<EventHistory> lstData)
    {
    }

我使用的是上面的列表作为参数(即"EventHistory"class类型的列表)如果我想传递所有类型的列表,那么一般如何创建列表参数?

如果要处理所有类型的列表,唯一通用的是 List<T> 中的 t:

public void GenerateExcelReport<T>(List<T> lstData)
{
    // ...
}

文档:Generic Methods

既然您已经提到您想要 "accept all the type of classes?",您可以添加一个约束以仅允许 类 而不是值类型:

public void GenerateExcelReport<T>(List<T> lstData) where T: class
{}

您需要将 T 的泛型类型参数添加到您的方法中:

 void GenerateExcelReport<T>(List<T> list){
 }

然后调用它:

    GenerateExcelReport(stringList);
    GenerateExcelReport(objectList);