title: C# Reflection - SetValue by path to property
body: <p>I'd like to update value in any public property by specifying a dots delimited path to it.</p>
  
  <p><strong>But whenever I call my method I receive an error in line:</strong></p>
  
  <pre><code>pi.SetValue(instance, value1, null);
  </code></pre>
  
  <p><strong>Error message:</strong></p>
  
  <p><em>Object does not match target type.</em></p>
  
  <p><strong>My method:</strong></p>
  
  <pre><code>private void SetPathValue(object instance, string path, object value)
  {
      string[] pp = path.Split('.');
      Type t = instance.GetType();
      for (int i = 0; i &lt; pp.Length; i++)
      {
          PropertyInfo pi = t.GetProperty(pp[i]);
          if (pi == null)
          {
              throw new ArgumentException("Properties path is not correct");
          }
          else
          {
              instance = pi.GetValue(instance, null);
              t = pi.PropertyType;
              if (i == pp.Length - 1)//last
              {
                 // Type targetType = IsNullableType(pi.PropertyType) ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;
                  var value1 = Convert.ChangeType(value, instance.GetType());
                  pi.SetValue(instance, value1, null);//ERROR
              }
          }
      }
  }
  
  private static bool IsNullableType(Type type)
  {
      return type.IsGenericType &amp;&amp; type.GetGenericTypeDefinition().Equals(typeof(Nullable&lt;&gt;));
  }
  </code></pre>