传递未知列表作为参数
本文关键字:参数 未知 列表 | 更新日期: 2023-09-27 18:07:58
在这里,我试图调用一个未知的List<T>
作为一个参数在我的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);
任何帮助都将非常感激。
可以将createReports()
设置为泛型方法:
private void createReports<T>(string Label, List<T> report)
{
//Do something...
}
假设您希望Do Something
根据类型的不同表现不同,您可能希望两个类实现一个接口:
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();
}
}
您是否希望两个方法调用相同的方法,因为对象在某种程度上是相关的,即共享一个共同的祖先?
如果你需要做的只是遍历报表,那么查看IEnumerable,它支持协方差。
private void createReports(string Label, IEnumerable<BaseClass> report)
{
// foreach (var r in report)
// Do something....
}
你好,我认为你可以使用IEnumerable来处理这个问题
所有的类都继承自"object"
也许你可以尝试使用
IEnumrable<object>