title: pass datatype of custom entity into function c#
body: <p>I am having four different Datatypes, all as Lists:</p>
  <pre><code>List&lt;DataRowTenant&gt; tenantlist;
  List&lt;DataRowOwner&gt; ownerlist;
  List&lt;DataRowCustomer&gt; customerlist;
  List&lt;DataRowHWDevice&gt; hwdevicelist;
  List&lt;DataRowHWCategory&gt; hwcategorslist;
  </code></pre>
  <p>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?</p>
  <p>i already tried the approaches in this <a href="https://stackoverflow.com/questions/10955579/passing-just-a-type-as-a-parameter-in-c-sharp">Thread</a>, but i couldn't get it to work.</p>
  <p>my CSV-Export function is as follows:</p>
  <pre><code>    public static void ExportOwnerCSV(List&lt;DataRowOwner&gt; list)
      {
          string columnNames = &quot;&quot;;
          string[] outputCsv = new string[list.Count + 1];
  
          if (list.Count &gt; 0)
          {
              SaveFileDialog sfd = new SaveFileDialog();
              sfd.Filter = &quot;CSV (*.csv)|*.csv&quot;;
              sfd.FileName = &quot;Output.csv&quot;;
              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(&quot;It wasn't possible to write the data to the disk.&quot; + 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] += &quot;\&quot;&quot; + item.Name + &quot;\&quot;&quot; + &quot;,&quot;;
                          }
  
                          //Body schreiben
                          int row = 1;
                          foreach (var DataRowitem in list)
                          {
                              foreach (PropertyInfo item in columnCount)
                              {
                                  outputCsv[row] += &quot;\&quot;&quot; + item.GetValue(list[row - 1]) + &quot;\&quot;&quot; + &quot;,&quot;;
                              }
                              row++;
                          }
  
                          File.WriteAllLines(sfd.FileName, outputCsv, Encoding.UTF8);
                          MessageBox.Show(&quot;Data Exported Successfully!&quot;, &quot;Info&quot;);
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show(&quot;Error :&quot; + ex.Message);
                      }
                  }
              }
          }
          else
          {
              MessageBox.Show(&quot;No Record To Export!&quot;, &quot;Info&quot;);
          }
      }
  </code></pre>
  <p>thanks, any suggestions are appreciated.</p>