Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@astander and @Frederik have essentially answered the questions and concerns that you specifically voiced, but I'd like to suggest doing things in a slightly more efficient manner. Depending on the number of object instances that you wish to write to your file, the method that you've presented may end up being quite inefficient. That's because you're gleaning type and value information via reflection on every iteration, which is unnecessary.</p> <p>What you're looking for is something that looks up type information once, and then only uses reflection to get the value of properties and fields, e.g. (.NET 3.5),</p> <pre><code>public static IEnumerable&lt;string&gt; ReturnRecs(IEnumerable items, bool returnHeader, string delimiter) { bool haveFoundMembers = false; bool haveOutputHeader = false; PropertyInfo[] properties = null; FieldInfo[] fields = null; foreach (var item in items) { if (!haveFoundMembers) { Type type = item.GetType(); properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(pi =&gt; pi.PropertyType.IsValueType || pi.PropertyType == typeof (string)).ToArray(); fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance) .Where(fi =&gt; fi.FieldType.IsValueType || fi.FieldType == typeof(string)).ToArray(); haveFoundMembers = true; } if (!haveOutputHeader) { yield return String.Join(delimiter, properties.Select(pi =&gt; pi.Name) .Concat(fields.Select(pi =&gt; pi.Name)).ToArray()); haveOutputHeader = true; } yield return String.Join(delimiter, properties.Select(pi =&gt; pi.GetValue(item, null).ToString()) .Concat(fields.Select(fi =&gt; fi.GetValue(item).ToString())).ToArray()); } </code></pre> <p>The above code only ever performs a <code>GetProperties</code> and <code>GetFields</code> once per <em>group of records</em>--also, because of this, there's no need to explicitly sort the properties and fields as was @Frederik's suggestion.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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