Results 1 to 3 of 3

Thread: [RESOLVED] Using reflection - get an individual item from a property that is an object array

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Resolved [RESOLVED] Using reflection - get an individual item from a property that is an object array

    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:
    1. public partial class UsageColumn {
    2.        
    3.         private string nameField;
    4.        
    5.         private string dataTypeField;
    6.        
    7.         private object[] valuesField;
    8.        
    9.  
    10.         public string Name {
    11.             get {
    12.                 return this.nameField;
    13.             }
    14.             set {
    15.                 this.nameField = value;
    16.             }
    17.         }
    18.        
    19.         public string DataType {
    20.             get {
    21.                 return this.dataTypeField;
    22.             }
    23.             set {
    24.                 this.dataTypeField = value;
    25.             }
    26.         }
    27.        
    28.         public object[] Values {
    29.             get {
    30.                 return this.valuesField;
    31.             }
    32.             set {
    33.                 this.valuesField = value;
    34.             }
    35.         }
    36.        
    37.         }
    38.     }

    I am currently turning this into a .NET collection thus:-


    C# Code:
    1. public static List<TRecord> ConvertResultToList<TRecord>(object[] columns, int rowCount) where TRecord : DTO.IDataScopeDTOClass
    2.         {
    3.             List<TRecord> ret = new List<TRecord>();
    4.  
    5.             // The object that we got in columns should have a property called Name and a property called DataType and Values[]
    6.             if (rowCount > 0)
    7.             {
    8.                 if (columns.Count() > 0)
    9.                 {
    10.                     System.Reflection.PropertyInfo piName = columns[0].GetType().GetProperty(@"Name" );
    11.                     System.Reflection.PropertyInfo piDataType = columns[0].GetType().GetProperty(@"DataType");
    12.                     System.Reflection.PropertyInfo piValues = columns[0].GetType().GetProperty(@"Values");
    13.  
    14.                     System.Reflection.ConstructorInfo ciTarget = typeof(TRecord).GetConstructor(Type.EmptyTypes );
    15.  
    16.  
    17.                     for (int i = 0; i < rowCount ; i++)
    18.                     {
    19.                         TRecord newValue = (TRecord)ciTarget.Invoke(null);
    20.                         foreach (var thisColumn in columns)
    21.                         {
    22.                             string name = piName.GetValue(thisColumn).ToString();
    23.                             string dataType = piDataType.GetValue(thisColumn).ToString();
    24.                             object[] value = (object[])piValues.GetValue(thisColumn);
    25.                             newValue.SetProperty(name, dataType, value[i]);
    26.                         }
    27.                         ret.Add(newValue);
    28.                     }
    29.  
    30.                 }
    31.  
    32.  
    33.             }
    34.  
    35.             return ret;
    36.         }


    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] ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using reflection - get an individual item from a property that is an object array

    Quote Originally Posted by Merrion View Post
    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] ?
    What exactly do you mean by "copying the whole array"? Do you just mean that you get a reference to the array because I wouldn't have thought that it would actually create a new array object and copy the elements of the existing one. An array is just a reference type object like any other so GetValue should simply provide you a reference to the object returned by the property getter, just like any other reference type property.

    If you simply mean that it gives you the array when you just want one element from the array then you would treat it like any other array. You have to get the array object first before you can get an element from it. For instance, the Lines property of a TextBox returns a String array. You can't just get one line from that array. You have to get the array and then get an element from it. You can discard the rest of the array if you want but you still gotta get it.

    Unlike TextBox.Lines though, your array isn't created on demand, so you're simply getting a reference to an object. It's no less efficient that doing the same for any other object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Using reflection - get an individual item from a property that is an object array

    True - this is more an aesthetic consideration than a performance issue...as it happens it is working well as is so I'll leave it well alone :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width