I have a data set that is in a peculiar setup - an array of "column" classes with properties "Name", "DataType" and "Values"
The latter property is an array of values e.g.
C# Code:
public partial class UsageColumn { private string nameField; private string dataTypeField; private object[] valuesField; public string Name { get { return this.nameField; } set { this.nameField = value; } } public string DataType { get { return this.dataTypeField; } set { this.dataTypeField = value; } } public object[] Values { get { return this.valuesField; } set { this.valuesField = value; } } } }
I am currently turning this into a .NET collection thus:-
C# Code:
public static List<TRecord> ConvertResultToList<TRecord>(object[] columns, int rowCount) where TRecord : DTO.IDataScopeDTOClass { List<TRecord> ret = new List<TRecord>(); // The object that we got in columns should have a property called Name and a property called DataType and Values[] if (rowCount > 0) { if (columns.Count() > 0) { System.Reflection.PropertyInfo piName = columns[0].GetType().GetProperty(@"Name" ); System.Reflection.PropertyInfo piDataType = columns[0].GetType().GetProperty(@"DataType"); System.Reflection.PropertyInfo piValues = columns[0].GetType().GetProperty(@"Values"); System.Reflection.ConstructorInfo ciTarget = typeof(TRecord).GetConstructor(Type.EmptyTypes ); for (int i = 0; i < rowCount ; i++) { TRecord newValue = (TRecord)ciTarget.Invoke(null); foreach (var thisColumn in columns) { string name = piName.GetValue(thisColumn).ToString(); string dataType = piDataType.GetValue(thisColumn).ToString(); object[] value = (object[])piValues.GetValue(thisColumn); newValue.SetProperty(name, dataType, value[i]); } ret.Add(newValue); } } } return ret; }
However the line object[] value = (object[])piValues.GetValue(thisColumn); is copying the whole array every time. How do I just get the value at index [i] ?


Reply With Quote

