mercredi 28 novembre 2018

Passing a new instance of a Type through a Generic Method

I have a series of very similar methods:

    private static DocumentBody GetPdfBodyObject(int sectionId)
    {
        DocumentBody db = new DocumentBody();
        // Add some stuff to Db
        return db;
    }

    private static DocumentHeader GetPdfHeaderObject(int sectionId)
    {
        DocumentHeader dh = new DocumentHeader();
        // Add some stuff to DH - similar to above
        return dh;
    }

And so on...

As you can see the difference between these two examples is based around the Type that is being instantiated & returned.

So instantly I thought to use a Generic Method to reduce the code duplication... I just cant seem to figure out which way is the 'best practice' and can I get what I need without using reflection?

I haven't used Generic Methods much at all so any advice is welcome.





Aucun commentaire:

Enregistrer un commentaire