I have some classes and some lists of objects of these classes. Example of a couple of those classes:
public class RipsAC
{
public string NumFactura { get; set; }
public string CodigoPrestador { get; set; }
public string TipoIdUsuario { get; set; }
public string NumIdUsuario { get; set; }
public DateTime FechaConsulta { get; set; }
}
public class RipsAH
{
public string DxComplicacion { get; set; }
public int EstadoSalida { get; set; }
public string DxCausaBasicaMuerte { get; set; }
public DateTime FechaEgresoInstitucion { get; set; }
public TimeSpan HoraEgresoInstitucion { get; set; }
}
I fill in lists of objects from these classes.
List<RipsAC> listaRipsAc = new List<RipsAC>();
List<RipsAH> listaRipsAh = new List<RipsAH>();
Now the problem is that I want to be able to send these lists to a common method using a parameter. But I don't know what type would be the parameter that I should add in the method.
public void ImprimirConsolidados(/*lista_Imprimir*/)
{
string archivoRipsUs = @"C:\TMP\Rips\US_" +
DateTime.Now.ToString("ddMMyyyy_hhmm") + ".csv";
if (!Directory.Exists(@"C:\TMP\Rips"))
{
Directory.CreateDirectory(@"C:\TMP\Rips");
}
using (var fileWriter = new StreamWriter(File.OpenWrite(archivoRipsUs), Encoding.UTF8))
using (var csvWriter = new CsvWriter(fileWriter))
{
csvWriter.WriteRecords(lista_Imprimir);
}
}
Can I group these lists in some container and be able to send them as a single parameter?
Define that it receives the lists with their object type:
This way you can call the method with the lists you created previously:
You could also define to receive a Generic list type:
Based on your question update you can convert the List to a generic List via
.Cast<Object>().ToList()
either
and your method would be:
The answer is yes, you just have to make a few small changes by receiving a generic type in the method
ImprimirConsolidados<T>(List<T> lista_Imprimir)
so that it looks like this:UPDATE
It is worth mentioning that now to invoke the method is as follows:
Or in her case
The type of conversion (cast) if it can be done. to be able to save any type of Objects.
And your method to Save the data to a csv file would be
Being that the list is only used to write it to a CSV file using the following statement:
...and since it
WriteRecords()
accepts a type parameterSystem.Collections.IEnumerable
(I recognize the use of the CsvHelper library from your earlier question ), then in this case you can use that same type as a parameter to your method:Doing it this way, you can pass the lists to the method without any cast: