I have the following final code:
SqlDataReader dataReader;
dataReader = cmd.ExecuteReader();
// var list = new List<string[]>();
List<T[]> list = new List<T[]>();
while (dataReader.Read())
{
// var row = new string[dataReader.FieldCount];
//object[] row new object;
T[] bass;
object[] row = new object[dataReader.FieldCount];
dataReader.GetValues(row);
bass = (T[]) Convert.ChangeType(row,typeof(T[]));
list.Add(bass);
}
I kept tried to convert an object array to a T generic type array in different ways, but i didn't make it. For the earlier code i get a runtime error: object must implement IConvertible. I put a constraint for the generic type when i defined the class:
class Cclass<T>: Iinterface<T> where T:IConvertible
{
//body
}
I'm open to other suggestions that can fulfill my goal of casting to generic array. Thank you!
You can use Linq:
bass = row
.Select(x => (T)Convert.ChangeType(x, typeof(T)))
.ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With