Q:

How to assign values to the nested objects dynamically?

I'm working on a project and I need to be able to do the following:
Say, We have Aa class that is configuration class:
public class Aa
{
    public Bb BbName { set; get; }
    public string Dd { set; get; }
}

public class Bb
{
    public string bb { set; get; }
    public string cc { set; get; }
}

I want to fetch variables from environment in order to assign them to the respective properties. Here is what I mean: as a simulation of Environment.GetEnvironmentVariable, let's use Dictionary<string, string>.
Setup will look like this:
var dic = new Dictionary<string, string>();
dic.Add("Dd", "236.154");
dic.Add("BbName.bb", "value_for_Bb.bb"); // as you can see, I'm using nested object's full
dic.Add("BbName.cc", "value_for_Bb.cc"); // path, in order to be able to identify it

Then I wrote a method, which looks like this:
private static void ReadPropertiesRecursive<T>(T obj, Type type, List<string> prefixes)
{

    foreach (PropertyInfo property in type.GetProperties())
    {
        if (property.PropertyType.GetTypeInfo().IsClass && property.PropertyType != typeof(string))
        {
            prefixes.Add(property.Name);
            ReadPropertiesRecursive(obj, property.PropertyType, prefixes);
            prefixes.Remove(property.Name);
        }
        else
        {
            var propertyFullName = prefixes != null && prefixes.Count > 0 ? $"{prefixes.Aggregate((i, j) => i + "." + j)}.{property.Name}" : property.Name;

            property.SetValue(obj, dic[propertyFullName]);

            Console.WriteLine(propertyFullName); // just for debugging
        }
    }
}

So, using recursion, I'm able to reach all nested objects of the given Type and the only thing that is left is assigning corresponding values to them. I'm trying to do that on property.SetValue(obj, dic[propertyFullName]);, but it throws an error, and it makes sense: obj is a "root" object, not the one that I want to assign values to, directly. So, I probably need to pass here not obj, but rather an instance of property, which is obj's property. Is that correct? Will that work? If yes, how should I do that?

A:

That did the trick:
private static void ReadPropertiesRecursive<Tt>(Tt obj, Type type, List<string> prefixes)
{

    foreach (PropertyInfo property in type.GetProperties())
    {
        if (property.PropertyType.GetTypeInfo().IsClass && property.PropertyType != typeof(string))
        {
            prefixes.Add(property.Name);
            var val = property.GetValue(obj);
            if (val == null)
                val = Activator.CreateInstance(property.PropertyType);
            property.SetValue(obj, val);
            ReadPropertiesRecursive(val, property.PropertyType, prefixes);
            prefixes.Remove(property.Name);
        }
        else
        {
            var propertyFullName = prefixes != null && prefixes.Count > 0 ? $"{prefixes.Aggregate((i, j) => i + "." + j)}.{property.Name}" : property.Name;

            property.SetValue(obj, dic[propertyFullName]);

            Console.WriteLine(propertyFullName);
        }
    }
}

