简体   繁体   English

将自定义实体的数据类型传递给 function c#

[英]pass datatype of custom entity into function c#

I am having four different Datatypes, all as Lists:我有四种不同的数据类型,都作为列表:

List<DataRowTenant> tenantlist;
List<DataRowOwner> ownerlist;
List<DataRowCustomer> customerlist;
List<DataRowHWDevice> hwdevicelist;
List<DataRowHWCategory> hwcategorslist;

And i want to be able to export them into a CSV.我希望能够将它们导出到 CSV 中。 Currently i copied my CSV-export Function five times, just with a different name and parameter definition.目前我复制了我的 CSV-export Function 五次,只是使用了不同的名称和参数定义。 So, i was asking myself if i can somehow identify the Datatype of a variable and pass that also in the function?所以,我问自己是否可以以某种方式识别变量的数据类型并将其传递到 function 中?

i already tried the approaches in this Thread , but i couldn't get it to work.我已经尝试过这个Thread中的方法,但我无法让它工作。

my CSV-Export function is as follows:我的 CSV 导出 function 如下:

    public static void ExportOwnerCSV(List<DataRowOwner> list)
    {
        string columnNames = "";
        string[] outputCsv = new string[list.Count + 1];

        if (list.Count > 0)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "CSV (*.csv)|*.csv";
            sfd.FileName = "Output.csv";
            bool fileError = false;
            bool? result = sfd.ShowDialog();
            if (result == true)
            {
                if (File.Exists(sfd.FileName))
                {
                    try
                    {
                        File.Delete(sfd.FileName);
                    }
                    catch (IOException ex)
                    {
                        fileError = true;
                        MessageBox.Show("It wasn't possible to write the data to the disk." + ex.Message);
                    }
                }
                if (!fileError)
                {
                    try
                    {
                        //var columnCount = DataRowOwner.GetType().GetFields();
                        var list_single = list[0];
                        var columnCount = list_single.GetType().GetProperties(BindingFlags.DeclaredOnly |
                                                                                    BindingFlags.Public |
                                                                                    BindingFlags.Instance);

                        //Header schreiben
                        foreach (PropertyInfo item in columnCount)
                        {
                            outputCsv[0] += "\"" + item.Name + "\"" + ",";
                        }

                        //Body schreiben
                        int row = 1;
                        foreach (var DataRowitem in list)
                        {
                            foreach (PropertyInfo item in columnCount)
                            {
                                outputCsv[row] += "\"" + item.GetValue(list[row - 1]) + "\"" + ",";
                            }
                            row++;
                        }

                        File.WriteAllLines(sfd.FileName, outputCsv, Encoding.UTF8);
                        MessageBox.Show("Data Exported Successfully!", "Info");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error :" + ex.Message);
                    }
                }
            }
        }
        else
        {
            MessageBox.Show("No Record To Export!", "Info");
        }
    }

thanks, any suggestions are appreciated.谢谢,任何建议表示赞赏。

You can declare the function as a generic function, where the type argument of the function is used as the type argument of the list parameter like so:您可以将 function 声明为通用 function,其中 function 的类型参数用作列表参数的类型参数,如下所示:

public static void ExportOwnerCSV<T>(IList<T> list)
{
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM