mardi 5 novembre 2019

Using reflection for model creation - Any alternatives?

I know there are tons of documents/blogs/discussions on how bad reflection is. Knowing that, let's move forward to the scenario:

Say we have a data-table/cursor (yes, those are still used). Now say, this has to be converted into a strongly types list of objects.

Option 1

Loop through the entire cursor/data table & manually assign everything to the instance of a class, and finally add that object to a list/collection.

Option 2

Have a method with a Generic class parameter and use reflection to generate the object of that class type.

ex.

var myList = convertDataTableToList<MyClass>(dataTable)

In this scenario, what are the disadvantages of reflection ?

Are there any alternatives to this approach (option 2) to achieve the same result?

My question is not language specific.

Note: The code example is in C#.net





Aucun commentaire:

Enregistrer un commentaire