DriverStaistic이라는 모델이 있고이 모델을 상속 한 뷰 모델 뷰 모델에 추가 속성이 포함되어 있지 않습니다. 이는 DriverStaistic의 빈 상속 일뿐입니다. 외부 라이브러리에서 모델 객체를 받고 최소 결합을 유지하려고합니다. 기본 클래스와 하위 클래스가 동일한 속성을 포함하고 있기 때문에 궁금한 점이 있다면 람다 식을 사용하는 것 이외에 다른 속성을 추가하거나 생략 할 때마다 수정해야한다는 것입니다.기본 모델을 하위 모델로 변환
리플렉션을 사용하여이 솔루션으로 출시되었지만 향후 성능 문제가 발생할 것으로 생각됩니다. 누구든지 더 나은 제안을 할 수 있습니까?
public static void FromDriverStatistic(this DriverStatisticsVm viewModel, object model)
{
bool isDriverStatistic = model is DriverStatistic;
if(!isDriverStatistic)
throw new InvalidCastException();
var modelProperties = model.GetType().GetProperties();
foreach (var property in modelProperties)
{
property.SetValue(viewModel, property.GetValue(model));
}
}
"외부 라이브러리에서 모델 객체를 받고 최소 결합을 유지하고 싶습니다."이렇게하면 더러운 해킹이 필요합니다. – Will