C# - Class Type as Parameter in methods

Posted by Claudio on Stack Overflow See other posts from Stack Overflow or by Claudio
Published on 2011-01-11T00:33:59Z Indexed on 2011/01/11 1:54 UTC
Read the original article Hit count: 346

Filed under:

Hi guys,

I'm using SQLite.cs wrapper for helping me with the database and I have this method for create XML from a table, that is working fine.

public void GenerateInvoiceXML(string filePath) {
        var invoices = app.db.Table<Invoice>().ToList();

        XmlSerializer serializer = new XmlSerializer( typeof(List<Invoice>) );
        TextWriter writer = new StreamWriter(filePath);

        serializer.Serialize(writer,invoices);
        writer.Close();

}

All tables that I have are defined like this:

[Serializable]  
public class Invoice
{

    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Supplier {get; set;}
    public string Date {get; set;}
    public string PaymentMethod {get; set;}
    public string Notes {get; set;}

    public Invoice(int newID)
    {
        Id = newID;
    }
    public  Invoice()
    {

    }
}

But I want to change this method for something like this:

public void GenerateInvoiceXML(string filePath, Type table) {
        var dataForXML = app.db.Table<table>().ToList();

        XmlSerializer serializer = new XmlSerializer( typeof(List<table>) );
        TextWriter writer = new StreamWriter(filePath);

        serializer.Serialize(writer,dataForXML);
        writer.Close();     
}

Does anybody have an idea how to do it?

Kind Regards,
Claudio

© Stack Overflow or respective owner

Related posts about c#