Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed a Better Way Than Reflection
    primarykey
    data
    text
    <p>I'm reading a CSV file and the records are recorded as a string[]. I want to take each record and convert it into a custom object.</p> <pre><code>T GetMyObject&lt;T&gt;(); </code></pre> <p>Currently I'm doing this through reflection which is really slow. I'm testing with a 515 Meg file with several million records. It takes under 10 seconds to parse. It takes under 20 seconds to create the custom objects using manual conversions with <code>Convert.ToSomeType</code> but around 4 minutes to do the conversion to the objects through reflection.</p> <p>What is a good way to handle this automatically?</p> <p>It seems a lot of time is spent in the <code>PropertyInfo.SetValue</code> method. I tried caching the properties <code>MethodInfo</code> setter and using that instead, but it was actually slower.</p> <p>I have also tried converting that into a delegate like the great Jon Skeet suggested here: <a href="https://stackoverflow.com/questions/1027980/improving-performance-reflection-what-alternatives-should-i-consider">Improving performance reflection , what alternatives should I consider</a>, but the problem is I don't know what the property type is ahead of time. I'm able to get the delegate</p> <pre><code>var myObject = Activator.CreateInstance&lt;T&gt;(); foreach( var property in typeof( T ).GetProperties() ) { var d = Delegate.CreateDelegate( typeof( Action&lt;,&gt; ) .MakeGenericType( typeof( T ), property.PropertyType ), property.GetSetMethod() ); } </code></pre> <p>The problem here is I can't cast the delegate into a concrete type like <code>Action&lt;T, int&gt;</code>, because the property type of <code>int</code> isn't known ahead of time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload