我编写了管理表单的 ASP.NET 页面。它们基于以下基类。
public abstract class FormPageBase<TInterface, TModel> : Page, IKeywordProvider
where TModel:ActiveRecordBase<MasterForm>, TInterface, new()
where TInterface:IMasterForm
{
public TInterface FormData { get; set; }
}
这里有一个示例子类:
public partial class PersonalDataFormPage : FormPageBase<IPersonalDataForm, PersonalDataForm>, IHasFormData<IPersonalDataForm>, IHasContact
{
}
下面我在页面上有一个用户控件,我想从页面“使用”“FormData”,以便它可以读取/写入它。
然后,我有一个更“通用”的用户控件,我想在我的所有表单子类的基本接口上进行操作...... IMasterForm
但是当用户控件尝试投射 Page.FormData (试图将页面投射到IHasFormData<IMasterForm>
它时告诉我该页面是IHasFormData<IFormSubclass>
即使我对 IFormSubclass 说它也是 IMasterForm
无论如何,我可以从泛型子类转换为泛型超类,还是这个“协方差”和 C# 4.0 的东西?
public abstract class FormControlBase<T> : UserControl, IKeywordProvider
where T:IMasterForm
{
protected T FormData { get; set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//This cast is failing when my common control's T does not exactly match
// the T of the Page.. even though the common controls TInterface is a base interface to the
//pages TInterface
FormData = ((IHasFormData<T>) Page).FormData;
if (!IsPostBack)
{
PopulateBaseListData();
BindDataToControls();
}
}
protected abstract void PopulateBaseListData();
protected abstract void BindDataToControls();
public abstract void SaveControlsToData();
#region IKeywordProvider
public List<IKeyword> GetKeywords(string categoryName)
{
if(!(Page is IKeywordProvider ))
throw new InvalidOperationException("Page is not IKeywordProvider");
return ((IKeywordProvider) Page).GetKeywords(categoryName);
}
#endregion
}