将未知List <t>作为参数传递?

时间:2016-09-19 21:14:54

标签: c# winforms

这里我尝试在单击按钮时在我的createReports方法中调用未知List<T>作为参数。当我点击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);

非常感谢任何帮助。提前谢谢。

4 个答案:

答案 0 :(得分:1)

您可以将createReports()作为通用方法:

private void createReports<T>(string Label, List<T> report)
{
    //Do something... 
}

答案 1 :(得分:0)

假设您希望 declare @table table(UserId int, Type_1_Status bit, Type_2_Status bit, Type_3_Status bit, Type_4_Status bit) insert into @table(UserId,Type_1_Status,Type_2_Status,Type_3_Status,Type_4_Status) select 1,1,0,0,0 union select 1,0,1,0,0 union select 1,0,0,1,0 union select 1,0,0,0,1 union all select 2,0,0,0,1 union all select 2,0,0,0,1 union all select 2,0,0,0,1 union all select 2,0,0,0,1 ;with report as( select UserId,'Status1' as Type,count(Type_1_Status) over(order by UserId) as Status from @table where Type_1_Status=1 union all select UserId,'Status2' as Type,count(Type_2_Status) over(order by UserId) from @table where Type_2_Status=1 union all select UserId,'Status3' as Type,count(Type_3_Status) over(order by UserId) from @table where Type_3_Status=1 union all select UserId,'Status4' as Type,count(Type_4_Status) over(order by UserId) from @table where Type_4_Status=1) select UserId,count(Status) from report where Status=1 group by UserId having count(Status) =4 根据类型有不同的行为,您可能希望让这两个类实现一个接口:

Do Something

答案 2 :(得分:0)

您是否希望两种方法都调用相同的方法,因为对象在某种程度上是相关的,即共享一个共同的祖先?

如果您需要做的只是遍历报告,那么请查看IEnumerable,它支持协方差。

private void createReports(string Label, IEnumerable<BaseClass> report)
{
    // foreach (var r in report) 
    // Do something....
}

答案 3 :(得分:0)

嗨,我认为您可以使用IEnumerable处理此问题

所有类均从“对象”继承

也许您可以尝试使用

IEnumrable<object>