Quantcast
Viewing all articles
Browse latest Browse all 1710

Performance issue with PropertyDescriptorCollection.GetValue()

Hi,

I have observed the performance issue with PropertyDescriptorCollection.GetValue() function. I am trying to implement the functionality where we export the data into the file. So I am calling below function in loop. The Bold lines are shown by profiler which is causing to reduce the performance. 


        internal void StreamWriter<T>(StreamWriter writer, List<T> data)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));

            object[] values = new object[props.Count];
            foreach (T item in data)
            {
                int nonEmptyFields = 0;
                int count = 1;
                for (int i = 0; i < values.Length; i++)
                {
                   var myvalue = props[i].GetValue(item);
                    if (myvalue != null && myvalue.ToString() != string.Empty)
                    {
                        nonEmptyFields++;
                    }
                }

                writer.Write(props[0].GetValue(item));
                for (int i = 1; i < values.Length; i++)
                {
                    var myItem = props[i].GetValue(item);
                    if (count < nonEmptyFields)
                    {
                       writer.Write("," + myItem);
                    }
                    if (myItem != null && myItem.ToString() != string.Empty)
                    {
                        count++;
                    }
                }
                writer.WriteLine(string.Empty);
            }
        }

Let me know how I can improve the performance in this case.

Thanks,

Ajit


Viewing all articles
Browse latest Browse all 1710

Trending Articles