我找到了一个解决方案ExpandoObject
- 给定一个只读模型对象,这会在运行时生成一个读写模型,每当任何属性发生变化时都会调用控制器方法:
public static dynamic GetAdapterFor(IController controller, object modelObj)
{
if (modelObj == null)
return null;
ExpandoObject obj = new ExpandoObject();
// add all the properties in the model
foreach (var prop in modelObj.GetType().GetProperties())
{
((IDictionary<string, object>)obj).Add(prop.Name, prop.GetValue(modelObj, null));
}
// add the handler to update the controller when a property changes
((INotifyPropertyChanged)obj).PropertyChanged += (s, e) => UpdateController(controller, e.PropertyName, ((IDictionary<string, object>)s)[e.PropertyName]);
return obj;
}
private static void UpdateController(IController controller, string propertyName, object propertyValue)
{
controller.SetPropertyValue(propertyName, propertyValue);
}