mercredi 23 juin 2021

How to handle list dynamically Using reflection and cast returned data to list

When I add a list in instances value and return that list its provides always duplicate value. I wanna read data from the database table values. But its first iterations add a single row value, the second time its provides 2nd row value and replaces the 1st list values.

var instance = (T)Activator.CreateInstance(type);

        while (reader.Read())
        {
           type.GetProperties().ToList().ForEach(property =>
            {
                property.SetValue(instance,reader[property.Name]);
            });
            list.Add(instance);  
        }
        return list;




Aucun commentaire:

Enregistrer un commentaire