I have class Settings
public class Settings
{
public string A{get;set;}
public bool B {get;set;}
public int C {get;set;}
}
I another class I have property type of Settings
public class VM
{
public class Settings Settings{get;set;}
}
I want setup values of property Settings with reflection.
I need pass argument type of object to InitializeSettings method.
public void Init(object viewModel)
{
try
{
PropertyInfo settings = viewModel.GetType().GetProperty("Settings");
PropertyInfo[] settingsProperties = settings.PropertyType.GetProperties();
foreach (PropertyInfo settingsProperty in settingsProperties)
{
object value = //load from app.config
var convertedValue = Convert.ChangeType(value, settingsProperty.PropertyType);
//how set value ???
settingsProperty.SetValue(settings, convertedValue, null);
}
}
catch (Exception exception)
{
throw;
}
}
This sample code finish with exception
base = {"Object does not match target type."}
I don’t know how can I set values on viewModel.Settings properties in Init method?