public static void Main()
Type t = Type.GetType("Test");
object objInstance = Activator.CreateInstance(t);
string propFullName = "OtherTest.Name";
ApplyValueToProperty(objInstance, t, propFullName, "Hello guys!");
string anotherProp = "OtherTest.AnotherTest.AnotherName";
ApplyValueToProperty(objInstance, t, anotherProp, "Hello again! Are you lost?");
Console.WriteLine(objInstance);
private static void ApplyValueToProperty(object entityInstance, Type instanceType, string propName, string propValue)
if (propName.Contains("."))
string[] splittedName = propName.Split('.');
PropertyInfo currentProp = instanceType.GetProperty(splittedName[0]);
Type nestedPropertyType = currentProp.PropertyType;
var otherObject = currentProp.GetValue(entityInstance);
otherObject = Activator.CreateInstance(nestedPropertyType);
currentProp.SetValue(entityInstance, otherObject);
ApplyValueToProperty(otherObject, otherObject.GetType(), string.Join(".", splittedName.Skip(1)), propValue);
SetSafePropertyValue(entityInstance, instanceType, propName, propValue);
private static void SetSafePropertyValue(object entityInstance, Type instanceType, string propName, string propValue)
PropertyInfo prop = instanceType.GetProperty(propName);
Type propType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
object safeValue = GetSafeValue(propValue, propType);
prop.SetValue(entityInstance, safeValue, null);
private static object GetSafeValue(string value, Type propType)
return (value == null || value == "null") ? null : Convert.ChangeType(value, propType);
public string AnotherName
public string Name { get; set; }
public AnotherTest AnotherTest { get; set; }
public MyTest OtherTest { get; set; }
public string Name { get; set; }
public override string ToString()
return "OtherTest.Name: " + OtherTest.Name + "\nOtherTest.AnotherTest.AnotherName: " + OtherTest.AnotherTest.AnotherName;