I have the following:
public interface IWidgetUpdated<in TViewModel> where TViewModel : BaseWidgetViewModel, new()
{
string SystemName { get; }
Widget UpdateFromViewModel(TViewModel viewModel);
}
public abstract class BaseCcWidget<TViewModel> : IWidgetUpdated<TViewModel> where TViewModel : BaseWidgetViewModel, new()
{
public abstract string SystemName { get; }
public virtual Widget UpdateFromViewModel(TViewModel viewModel)
{
var widget = _widgetService.GetById(viewModel.Id) ?? new Widget();
widget.Name = viewModel.DisplayName;
if (widget.Id == 0) //Don't allow changing of widget type as set data might be incompatible.
widget.SystemName = viewModel.SystemName;
return widget;
}
}
public class StaticContentWidgetViewModel : BaseWidgetViewModel
{
//Code removed for brevity
}
public class StaticContentWidget : BaseCcWidget<StaticContentWidgetViewModel>
{
//Do implementations - removed for brevity
}
public IEnumerable<IWidgetUpdated<BaseWidgetViewModel>> GetUpdatableWidgets()
{
var result = new List<IWidgetUpdated<BaseWidgetViewModel>>();
var types = _typeFinder.FindClassesOfType(typeof(IWidgetUpdated<>));
foreach (Type drType in types)
{
var obj = EngineContext.Current.ResolveUnregistered(drType); //Creates an instance of StaticContentWidget
result.Add(obj as IWidgetUpdated<BaseWidgetViewModel>);
}
return result;
}
The issue I have is obj as IWidgetUpdated<BaseWidgetViewModel>
doesn't work. I use the same code on covariant interfaces with success but it won't work in this instance.
Basically, I just need to be able to call obj.UpdateFromViewModel()
. I've tried various casts without success.
Aucun commentaire:
Enregistrer un commentaire