title: nested properties error on string with string
body: <p>I have 2 questions</p>
  
  <ol>
  <li><p>Is there a way to initialize a class and all subclasses so all elements of the new item in the code are null? </p></li>
  <li><p>How do I handle a list of type Items. EmbeddedGenerator  and stop the error</p>
  
  <pre><code> Object reference not set to an instance of an object.
     at ConsoleApplication1.Program.SetProperty(String compoundProperty,
     Object target, String value)
     at ConsoleApplication1.Program.Main(String[] args)
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
     String[] args)
     at System.AppDomain.ExecuteAssembly(String assemblyFile,
     Evidence assemblySecurity, String[] args)
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     at System.Threading.ExecutionContext.Run
     (ExecutionContext executionContext, ContextCallback callback,
      Object state, Boolean ignoreSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext
     executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <p>(new System.Collections.Generic.Mscorlib_CollectionDebugView(((System.Collections.Generic.List)(target)))).Items[0].id.id_high</p></li>
  </ol>
  
  <p>Example "class1.properity1 = 1"</p>
  
  <p>I have run into an error here is a short console app outlining  what the error is 
  I'm trying to set the value of id_high to 1 the path is generator.contents[0].id.id_high</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using Items;
      using System.Reflection;
  
      namespace ConsoleApplication1
      {
       class Program
       {
        static List&lt;SavedItem&gt; items = new List&lt;SavedItem&gt;();
        static void Main(string[] args)
        {
         Items.SavedItem item = new Items.SavedItem();
         item.generator = new Items.Generator();
         item.generator.contents = new List&lt;Items.EmbeddedGenerator&gt;();
         item.generator.contents.Add(new Items.EmbeddedGenerator());
         item.generator.contents[0].id = new OnlineService.ItemId();
  
         SetProperty(".generator.contents[0].id.id_high", item, "1");
  
         items.Add(item);
        }
  
        //jon skeet's modified method
        public static void SetProperty(string compoundProperty, 
        object target, string value)
        {
         string[] bits = compoundProperty.Split('.');
         for (int i = 0; i &lt; bits.Length - 1; i++)
         {
          if (bits[i].IndexOf('[') &gt; 0)
          {
           bits[i] = bits[i].Substring(0, bits[i].IndexOf('['));
          }
  
          PropertyInfo propertyToGet = target.GetType().GetProperty(bits[i]);
          target = propertyToGet.GetValue(target, null); 
         }
         if (bits[bits.Length - 1].IndexOf('[') &gt; 0)
         {
          bits[bits.Length - 1] = bits[bits.Length - 1].Substring
          (0, bits[bits.Length - 1].IndexOf('['));
          value = value.Trim('{', '}');
  
          PropertyInfo lstpropertyToSet = target.GetType().GetProperty(bits.Last());
          if (value != "null")
          {
  
           List&lt;int&gt; list = new List&lt;int&gt;();
           foreach (string s in value.Split(','))
           {
            list.Add(Int32.Parse(s));
           }
           lstpropertyToSet.SetValue(target, 
            Convert.ChangeType(list, lstpropertyToSet.PropertyType)
            , null);
          }
  
          return;
         }
         PropertyInfo propertyToSet = target.GetType().GetProperty(bits.Last());
         if (value != "null")
         {
          propertyToSet.SetValue(target,
           Convert.ChangeType(value, propertyToSet.PropertyType),
           null);
         }
         return;
        }
  
       }
      }
  
  
     //....Items has an id_high separate from program   
      namespace Items
      {
       public partial class SavedItem
       {
        public SavedItem() { }
        private Items.Generator _generator = null;
        [global::System.ComponentModel.DefaultValue(null)]
        public Items.Generator generator
        {
         get { return _generator; }
         set { _generator = value; }
        }
        //....
       }
       public partial class Generator 
       {
        public Generator() { }
        private System.Collections.Generic.List&lt;EmbeddedGenerator&gt; _contents = 
         new System.Collections.Generic.List&lt;EmbeddedGenerator&gt;();
        public System.Collections.Generic.List&lt;EmbeddedGenerator&gt; contents
        {
         get { return _contents; }
         set { _contents = value; }
        }
        //.....
       }
       public partial class EmbeddedGenerator
       {
        public EmbeddedGenerator() { }
  
        private OnlineService.ItemId _id;
        public OnlineService.ItemId id
        {
         get { return _id; }
         set { _id = value; }
        }
        //....
       }
      }
      //....onlineService has an id_high separate from ConsoleApplication1 and Items
      namespace OnlineService
      {
       public partial class ItemId 
       {
        public ItemId() { }
  
        private ulong _id_high;
        public ulong id_high
        {
         get { return _id_high; }
         set { _id_high = value; }
        }
       }
       //....
      }
  </code></pre>
  
  <p>This code will run normally until it gets to the contents collection answering the first question might solve my problem but I don't think it will because not all properties need a value and will cause issues if there is one.</p>
  
  <p>Edit:
  Adding this helped add  the property to the target 
  But now "Cannot apply indexing with [] to an expression of type 'object'"<br>
   When you try to make target see item 1</p>
  
  <p>item.generator.contents.Add(new Items.EmbeddedGenerator());
          item.generator.contents[0].id = new OnlineService.ItemId();</p>