title: Mapping a string Array to an Object using ValueInjecter
body: <p>I'm looking for suggestions on how I might solve the following problem using ValueInjecter.</p>
  
  <p><strong>NOTE:</strong> Before anyone jumps in and <em>a)</em> tells me I'm doing it wrong or <em>b)</em> tells me I shouldn't be using ValueInjecter - please don't. beacuse <em>a)</em> I have no control over the source data and <em>b)</em> ValueInjecter is used everywhere else in the project, for every other data morphing task so using a different toolkit is really not an option)</p>
  
  <h1>The Problem</h1>
  
  <p>From a set of 3rd party service classes, I receive inbound data that is formatted as a keyed string array.  That is, my inbound object looks something like this:</p>
  
  <pre><code>var inboundData  = string[];
  var field1 = inboundData["field1"];
  var field2 = inboundData["field2"];
  var field3 = inboundData["field3"];
  var field4 = inboundData["field4"];
  </code></pre>
  
  <p>(<em>note: this is not real data, just a mockup of what I'm dealing with</em>)</p>
  
  <p>The main premise here is that to get at a field in the inbound data i either have to specify a field name, or an array index, I can't change this as the library that provides the data is supplied by a 3rd party who won't change things, and which our licence doesn't permit me to change.</p>
  
  <p>Each inbound data packet contains only the fields for one record, so if I have multiple rows to deal with what I actually have is an array of arrays, but I'm not worried about transforming that just yet, I want to tackle transforming only a single record first.</p>
  
  <p>What I need to do therefore, is to take:</p>
  
  <pre><code>var inboundData  = string[];
  var field1 = inboundData["field1"];
  var field2 = inboundData["field2"];
  var field3 = inboundData["field3"];
  var field4 = inboundData["field4"];
  </code></pre>
  
  <p>and transform it to:</p>
  
  <pre><code>public class inboundData
  {
    public string field1 { get; set; }
    public string field2 { get; set; }
    public string field3 { get; set; }
    public string field4 { get; set; }
  }
  </code></pre>
  
  <p>if possible, I'd like to be able to do this using ValueInjecter so that array key <strong>["blah"]</strong> is mapped to object property <strong>blah</strong> automatically should any field names be changed.</p>
  
  <h1>The Current Solution</h1>
  
  <p>At the moment, the data is transformed, but using the old fashioned method of:</p>
  
  <pre><code>var inboundData = inboundService.GetNextRecord();
  InboudDataObject = new InboundDataObject
  {
    field1 = inboundData["field1"],
    field2 = inboundData["field2"],
    field3 = inboundData["field3"],
    field4 = inboundData["field4"]
  }
  </code></pre>
  
  <p>While this works ok, there are going to be problems in future, and as anyone who's done this before will know it's a maintenance nightmare waiting to happen.</p>
  
  <p>I'd therefore like to be able to just do something like:</p>
  
  <pre><code>var inboundData = inboundService.GetNextRecord();
  InboudDataObject = new InboundDataObject().InjectFrom(inboundData);
  </code></pre>
  
  <p>So that if the 3rd party add a field to their record all I have to do is add a field of that name to my object.</p>
  
  <p>Right now, I have a couple of experiments I'm going to try, but after a day or twos worth of research I've not turned up anything concrete, I've found a couple of posts of performing a similar task using Auto-Mapper, but I'm not using Auto-Mapper so at best all I get from them is a few ideas (That's where my Experiments came from).</p>
  
  <p>So there we have it, anyone out there have any ideas/thoughts on how to tackle this one?</p>
  
  <p>Cheers
  Shawty</p>
  
  <h1>Update 7/5/2015</h1>
  
  <p>So about 24 hours later, "Omu" the creator of ValueInjecter solves my problem in one go (Many Thanks), I though however I would share the approach I'd been toying with since I posted this question, even though it's a little incomplete.</p>
  
  <p>After reading various things last night, I decided to experiment with trying to fill a dynamic object from the string array, with the intention that I could then just do a property to property injection from the anonymous/dynamic object into my concrete class.</p>
  
  <p>This approach would actually have worked except for one problem "<em>not being able to run extension methods on dynamic objects.</em>" however, some one out there may very well figure out the final piece of the puzzle, so here's my experimental code, so folks can see where my thoughts where:</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Dynamic;
  using Omu.ValueInjecter;
  
  namespace dynamic_mapping_test
  {
    public class DestinationObject
    {
      public string field1 { get; set; }
      public int field2 { get; set; }
      public string[] field3 { get; set; }
      public bool field4 { get; set; }
      public double field5 { get; set; }
    }
  
    class Program
    {
      private static Dictionary&lt;string, object&gt; sourceData = new Dictionary&lt;string, object&gt;();
  
      static void Main()
      {
        sourceData["field1"] = "A person";
        sourceData["field2"] = 20;
        sourceData["field3"] = new string[] {"A thing", "A thing"};
        sourceData["field4"] = false;
        sourceData["field5"] = 999.999;
  
        var eo = new ExpandoObject();
        var eoCollection = (ICollection&lt;KeyValuePair&lt;string, object&gt;&gt;) eo;
  
        foreach (var inputObject in sourceData)
        {
          eoCollection.Add(inputObject);
        }
  
        dynamic eoDynamic = eo;
  
        DestinationObject myObject = new DestinationObject().InjectFrom((object)eoDynamic) as DestinationObject;
      }
  
    }
  }
  </code></pre>
  
  <p>I'm marking Omu's post as the answer though, because that solves the problem now, the rest is just <strong>food for thought</strong></p>