简体   繁体   中英

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. Currently i copied my CSV-export Function five times, just with a different name and parameter definition. So, i was asking myself if i can somehow identify the Datatype of a variable and pass that also in the function?

i already tried the approaches in this Thread , but i couldn't get it to work.

my CSV-Export function is as follows:

    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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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