C# 通过未知列表<;T>;作为参数?

C# 通过未知列表<;T>;作为参数?,c#,winforms,C#,Winforms,在这里,我试图在我的createReports方法中通过单击按钮调用未知的列表。当我点击button1时,我需要一个来自CustomersInfo类的列表,当点击button2时,我想要一个来自ExpenseInfo类的列表。 这可能吗 代码已更新 class CustomersInfo { public string Name { get; set; } public st

在这里,我试图在我的createReports方法中通过单击按钮调用未知的
列表
。当我点击
button1
时,我需要一个来自
CustomersInfo
类的列表,当点击
button2
时,我想要一个来自
ExpenseInfo
类的列表。 这可能吗

代码已更新

class CustomersInfo
    {
        public string Name
        {
            get;
            set;
        }

        public string Amount
        {
            get;
            set;
        }
    }

class ExpenseInfo
    {
        public string Category
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }

        public string Amount
        {
            get;
            set;
        }
    }

List<CustomersInfo> customerInfo = new List<CustomersInfo>();
List<ExpenseInfo> expenseInfo = new List<ExpenseInfo>();

private void button1_Click(object sender,EventArgs e)
    {
        createReports(button1.Text, customerInfo);
    }

private void button2_Click(object sender, EventArgs e)
    {
        createReports(button2.Text, expenseInfo);
    }

viewForm.documentViewer1.CloseDocument();
        string fileName = Application.StartupPath + "\\**\\report.docx";
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
        try
        {
            var application = new Microsoft.Office.Interop.Word.Application();
            var distinct = report.Distinct().GroupBy(x => x.Name).Select(y => y.First());
            var document = application.Documents.Add(Template: Application.StartupPath + "/**/Templates/Reports.docx");
            total = 0;
            foreach (Microsoft.Office.Interop.Word.Field field in document.Fields)
            {
                if (field.Code.Text.Contains("Info"))
                {
                    field.Select();
                    application.Selection.TypeText(Label);
                }
                else if (field.Code.Text.Contains("Grid"))
                {
                    field.Select();



                    int RowCount = distinct.Count();
                    int ColumnCount = type.GetProperties().Length;
                    Object[,] DataArray = new object[RowCount, ColumnCount + 1];

                    //add rows
                    int r = 0;
                    int d = 0;

                    foreach (var client in distinct)
                    {
                        clientTotal = 0;
                        foreach (var info in report)
                        {
                            if (client.Name == info.Name)
                            {
                                clientTotal = Convert.ToDecimal(info.Amount.Remove(0, 1)) + clientTotal;
                            }
                        }
                        total = total + clientTotal;


                        DataArray[r, 0] = client.Name;
                        DataArray[r++, 1] = clientTotal.ToString("C2");
                    }

                    //page orintation
                    document.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;


                    dynamic oRange = document.Content.Application.Selection.Range;
                    string oTemp = "";
                    for (r = 0; r <= RowCount - 1; r++)
                    {
                        for (int c = 0; c <= ColumnCount - 1; c++)
                        {
                            oTemp = oTemp + DataArray[r, c] + "\t";

                        }
                    }

                    //table format
                    oRange.Text = oTemp;

                    object Separator = Microsoft.Office.Interop.Word.WdTableFieldSeparator.wdSeparateByTabs;
                    object ApplyBorders = true;
                    object AutoFit = true;
                    object AutoFitBehavior = Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitFixed;

                    oRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount,
                                          Type.Missing, Type.Missing, ref ApplyBorders,
                                          Type.Missing, Type.Missing, Type.Missing,
                                          Type.Missing, Type.Missing, Type.Missing,
                                          Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing);

                    oRange.Select();

                    document.Application.Selection.Tables[1].Select();
                    document.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
                    document.Application.Selection.Tables[1].Rows.Alignment = 0;
                    document.Application.Selection.Tables[1].Rows[1].Select();
                    document.Application.Selection.InsertRowsAbove(1);
                    document.Application.Selection.Tables[1].Rows[1].Select();

                    //header row style
                    document.Application.Selection.Tables[1].Rows[1].Range.Bold = 1;
                    document.Application.Selection.Tables[1].Rows[1].Range.Font.Name = "Arial";
                    document.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 11;

                    //add header row manually
                    document.Application.Selection.Tables[1].Cell(1, 1).Range.Text = "Client";
                    document.Application.Selection.Tables[1].Cell(1, 2).Range.Text = "Amount";

                    //table style 
                    document.Application.Selection.Tables[1].set_Style("Grid Table 4 - Accent 1");
                    for (int c = 1; c <= document.Application.Selection.Tables[1].Rows.Count - 1; c++)
                    {
                        document.Application.Selection.Tables[1].Rows[c].Range.Font.Size = 9;
                    }

                    document.Application.Selection.Tables[1].Rows[1].Select();
                    document.Application.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                }
                else if (field.Code.Text.Contains("total"))
                {
                    field.Select();
                    application.Selection.TypeText("Total Income by Client " + total.ToString("C2"));
                }
            }

            document.SaveAs(fileName);
            document.Close();
            application.Quit();
            application = null;
            document = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        catch (Exception ex)
        {
            // Get stack trace for the exception with source file information
            var st = new StackTrace(ex, true);
            // Get the top stack frame
            var frame = st.GetFrame(st.FrameCount - 1);
            // Get the line number from the stack frame
            var line = frame.GetFileLineNumber();
            MessageBox.Show(line.ToString());
        }
        showReports(Label);
class CustomersInfo
{
公共字符串名
{
收到
设置
}
公共字符串金额
{
收到
设置
}
}
课程费用信息
{
公共字符串类别
{
收到
设置
}
公共字符串名
{
收到
设置
}
公共字符串金额
{
收到
设置
}
}
List customerInfo=新列表();
列表费用信息=新列表();
私有无效按钮1\u单击(对象发送者,事件参数e)
{
createReports(button1.Text,customerInfo);
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
创建报告(button2.Text,expenseInfo);
}
viewForm.documentViewer1.CloseDocument();
字符串文件名=Application.StartupPath+“\\**\\report.docx”;
if(File.Exists(fileName))
{
删除(文件名);
}
尝试
{
var application=new Microsoft.Office.Interop.Word.application();
var distinct=report.distinct().GroupBy(x=>x.Name)。选择(y=>y.First());
var document=application.Documents.Add(模板:application.StartupPath+“/**/Templates/Reports.docx”);
总数=0;
foreach(document.Fields中的Microsoft.Office.Interop.Word.Field字段)
{
if(field.Code.Text.Contains(“信息”))
{
field.Select();
应用。选择。打印文本(标签);
}
else if(field.Code.Text.Contains(“网格”))
{
field.Select();
int RowCount=distinct.Count();
int ColumnCount=type.GetProperties().Length;
Object[,]DataArray=新对象[RowCount,ColumnCount+1];
//添加行
int r=0;
int d=0;
foreach(独立的var客户端)
{
clientTotal=0;
foreach(报告中的var信息)
{
if(client.Name==info.Name)
{
clientTotal=Convert.ToDecimal(info.Amount.Remove(0,1))+clientTotal;
}
}
总计=总计+客户总计;
DataArray[r,0]=client.Name;
DataArray[r++,1]=clientTotal.ToString(“C2”);
}
//页面定位
document.PageSetup.Orientation=Microsoft.Office.Interop.Word.WdOrientation.wdorientgrait;
动态橙色=document.Content.Application.Selection.Range;
字符串oTemp=“”;

对于(r=0;r您可以将
createReports()
作为通用方法:

private void createReports<T>(string Label, List<T> report)
{
    //Do something... 
}
private void createReports(字符串标签、列表报告)
{
//做点什么。。。
}

假设您希望
根据类型执行不同的操作
,您可能希望两个类都实现一个接口:

public interface IReportable {
    string GetReportLine();
}

public class CustomersInfo : IReportable {
    string IReportable.GetReportLine() {
        // Get the line for your report
    }
}
public class ExpenseInfo : IReportable {
    string IReportable.GetReportLine() {
        // Get the line for your report
    }
}

private void CreateReports(string Label, IEnumerable<IReportable> report) {
    foreach (IReportable info in report) {
        string line = info.GetReportLine();
    }
}
公共接口{
字符串GetReportLine();
}
公共类CustomerInfo:IReportable{
字符串IReportable.GetReportLine(){
//为你的报告排队
}
}
公共类费用信息:IReportable{
字符串IReportable.GetReportLine(){
//为你的报告排队
}
}
私有void CreateReports(字符串标签、IEnumerable报告){
foreach(报告中的可移植信息){
string line=info.GetReportLine();
}
}

是否希望两个方法调用相同的方法,因为对象之间存在某种关联,即共享一个共同的祖先

如果您所需要做的只是迭代报告,那么就查看IEnumerable,它支持协方差

private void createReports(string Label, IEnumerable<BaseClass> report)
{
    // foreach (var r in report) 
    // Do something....
}
private void createReports(字符串标签,IEnumerable报告)
{
//foreach(报告中的var r)
//做点什么。。。。
}

您好,我想您可以使用IEnumerable来处理这个问题

所有类都继承自“对象”

也许你可以试着用它

IEnumrable<object>
IEnumerable

理想情况下,你应该有一个公共的基类型。如果没有类型化列表,看看
//做点什么…
实际上是什么会很有趣。你将如何与列表项的成员交互?我将添加代码。这并不能真正完成任何事情,因为此方法的主体不知道任何关于
的内容
,因此它实际上与传入类似于
IList
ArrayList
的内容没有什么区别。如果您有一个通用约束,这将添加一些功能。这两个类之间没有任何关系,因为您可以看到每个类都是不同的