I'm trying to set Address1 in obj1 using reflection and I can't figure out how to get a reference to the correct object. I'm not sure how to get a reference to the Address1 instance to pass into the first parameter of SetValue()

**Round 1:** 

```
public class StackOverflowReflectionTest
    {
        [Fact]
        public void SetDeepPropertyUsingReflection()
        {
            var breadCrumb = ".Addresses[0].Address1";

            var obj1 = new Person()
            {
                Name = "Eric",
                Addresses = new List<Address>()
                {
                    new Address() {Address1 = "123 First Street"}
                }
            };

            var newAddress1 = "123 Second Street";

            var propNames = breadCrumb.Split(".");
            for (var index = 0; index < propNames.Length; index++)
            {
                var propName = propNames[index];
                if (propName.Contains("["))
                {
                    var propNameToGet = propName.Substring(0, propName.IndexOf("[", StringComparison.Ordinal));
                    var prop = obj1.GetType().GetProperty(propNameToGet);
                    var leftBrace = propName.IndexOf("[", StringComparison.Ordinal);
                    var rightBrace = propName.IndexOf("]", StringComparison.Ordinal);
                    var position = int.Parse(propName.Substring(leftBrace + 1, rightBrace - leftBrace - 1));

                    var propNameToSet = propNames[index + 1];

                    var propToSet = prop.PropertyType.GetGenericArguments()[position].GetProperty(propNameToSet);
                    propToSet.SetValue(obj1, newAddress1);
                }
                else
                {
                    //TODO: deal with different types
                }
            }
        }

        public class Person
        {
            public string Name { get; set; }
            public IList<Address> Addresses { get; set; }
        }

        public class Address
        {
           public string Address1 { get; set; }
        }
    }

```

---

**Round 2** based on Ed's feedback, still stuck on how to get the value on this line: var value = property.GetValue(obj, new object[] { indexPart });

```
  public class StackOverflowReflectionTest
    {
        [Fact]
        public void SetDeepPropertyUsingReflectionRound2()
        {
            var breadCrumb = "Addresses[0].Address1";

            var obj1 = new Person()
            {
                Name = "Eric",
                Addresses = new List<Address>()
                {
                    new Address() {Address1 = "123 First Street"}
                }
            };

            var newAddress1 = "123 Second Street";

            SetPropertyValueByPath(obj1, breadCrumb, newAddress1);
        }

        public bool CrackPropertyName(string name, out string namePart, out object indexPart)
        {
            if (name.Contains("["))
            {
                namePart = name.Substring(0, name.IndexOf("[", StringComparison.Ordinal));

                var leftBrace = name.IndexOf("[", StringComparison.Ordinal);
                var rightBrace = name.IndexOf("]", StringComparison.Ordinal);

                indexPart = name.Substring(leftBrace + 1, rightBrace - leftBrace - 1);
                return true;
            }
            else
            {
                namePart = name;
                indexPart = null;
                return false;
            }
        }

        public object GetPropertyValue(object obj, string name)
        {
            if(CrackPropertyName(name, out var namePart, out var indexPart))
            {
                var property = obj.GetType().GetProperty(namePart);
                var value = property.GetValue(obj, new object[] { indexPart });
                return value;
            }
            else
            {
                return obj.GetType().GetProperty(name);
            }

        }

        public void SetPropertyValue(object obj, string name, object newValue)
        {
            var property = typeof(Address).GetProperty(name);
            property.SetValue(obj, newValue);
        }

        public void SetPropertyValueByPath(object obj, string path, object newValue)
        {
            var pathSegments = path.Split(".");

            if (pathSegments.Length == 1)
            {
                SetPropertyValue(obj, pathSegments[0], newValue);
            }
            else
            {
                ////  If more than one remaining segment, recurse

                var child = GetPropertyValue(obj, pathSegments[0]);

                SetPropertyValueByPath(child, String.Join(".", pathSegments.Skip(1)), newValue);
            }
        }

        public class Person
        {
            public string Name { get; set; }
            public IList<Address> Addresses { get; set; }
        }

        public class Address
        {
           public string Address1 { get; set; }
        }
}

```

**Solution:**

```
public class StackOverflowReflectionTest
    {
[Fact]
        public void SetDeepPropertyUsingReflectionSolution()
        {
            var breadCrumb = "Addresses[0].Address1";

            var obj1 = new Person()
            {
                Name = "Eric",
                Addresses = new List<Address>()
                {
                    new Address() {Address1 = "123 First Street"}
                }
            };

            var newAddress1 = "123 Second Street";

            SetPropertyValueByPath(obj1, breadCrumb, newAddress1);
        }

        public bool CrackPropertyName(string name, out string namePart, out object indexPart)
        {
            if (name.Contains("["))
            {
                namePart = name.Substring(0, name.IndexOf("[", StringComparison.Ordinal));

                var leftBrace = name.IndexOf("[", StringComparison.Ordinal);
                var rightBrace = name.IndexOf("]", StringComparison.Ordinal);

                indexPart = name.Substring(leftBrace + 1, rightBrace - leftBrace - 1);
                return true;
            }
            else
            {
                namePart = name;
                indexPart = null;
                return false;
            }
        }

        public object GetPropertyValue(object obj, string name)
        {
            if(CrackPropertyName(name, out var namePart, out var indexPart))
            {

                var property = obj.GetType().GetProperty(namePart);
                var list = property.GetValue(obj);
                var value = list.GetType().GetProperty("Item").GetValue(list, new object[] { int.Parse(indexPart.ToString()) });
                return value;
            }
            else
            {
                return obj.GetType().GetProperty(namePart);
            }

        }

        public void SetPropertyValue(object obj, string name, object newValue)
        {
            var property = typeof(Address).GetProperty(name);
            property.SetValue(obj, newValue);
        }

        public void SetPropertyValueByPath(object obj, string path, object newValue)
        {
            var pathSegments = path.Split(".");

            if (pathSegments.Length == 1)
            {
                SetPropertyValue(obj, pathSegments[0], newValue);
            }
            else
            {
                ////  If more than one remaining segment, recurse
                var child = GetPropertyValue(obj, pathSegments[0]);

                SetPropertyValueByPath(child, String.Join(".", pathSegments.Skip(1)), newValue);
            }
        }

        public class Person
        {
            public string Name { get; set; }
            public IList<Address> Addresses { get; set; }
        }

        public class Address
        {
           public string Address1 { get; set; }
        }
}

```