It's not copying the array; it's just getting a reference to the existing array.
You could retrieve a single value using reflection, but that would almost certainly be slower than casting the value to an array and accessing it directly:
System.Reflection.MethodInfo miGet = piValues.PropertyType.GetMethod("Get");
...
object values = piValues.GetValue(thisColumn);
object value = miGet.Invoke(values, new object[] { i });
newValue.SetProperty(name, dataType, value);
Other comments:
A call to
GetType()
is slow; you should cache and re-use the result of
columns[0].GetType()
:
Type columnType = columns[0].GetType();
System.Reflection.PropertyInfo piName = columnType.GetProperty(@"Name" );
System.Reflection.PropertyInfo piDataType = columnType.GetProperty(@"DataType");
System.Reflection.PropertyInfo piValues = columnType.GetProperty(@"Values");
You can avoid calling the
TRecord
's constructor via reflection by adding a
new()
constraint to the type parameter:
where TRecord : DTO.IDataScopeDTOClass, new()
...
TRecord newValue = new TRecord();