Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this a covariance problem? Not sure if brick wall
    primarykey
    data
    text
    <p>I wrote ASP.NET pages which will manage forms. They're based on the following base class.</p> <pre><code>public abstract class FormPageBase&lt;TInterface, TModel&gt; : Page, IKeywordProvider where TModel:ActiveRecordBase&lt;MasterForm&gt;, TInterface, new() where TInterface:IMasterForm { public TInterface FormData { get; set; } } </code></pre> <p>And a sample SubClass is here:</p> <pre><code>public partial class PersonalDataFormPage : FormPageBase&lt;IPersonalDataForm, PersonalDataForm&gt;, IHasFormData&lt;IPersonalDataForm&gt;, IHasContact { } </code></pre> <p>Below I have a usercontrol on the page which I want to "consume" the "FormData" from the page so that it can read/write to it.</p> <p>I then, have a more "common" user control that I want to have operate on the base Interface of all my form subclasses... IMasterForm </p> <p>But when the usercontrol tries casting Page.FormData (having tried to cast page to <code>IHasFormData&lt;IMasterForm&gt;</code> it tells me that the page is <code>IHasFormData&lt;IFormSubclass&gt;</code> even though I have a constraint on the IFormSubclass that says it is also IMasterForm</p> <p>Is there anyway that i can cast from the generic subclass to the generic superclass or is this "covariance" and a C# 4.0 thing?</p> <pre><code>public abstract class FormControlBase&lt;T&gt; : 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&lt;T&gt;) Page).FormData; if (!IsPostBack) { PopulateBaseListData(); BindDataToControls(); } } protected abstract void PopulateBaseListData(); protected abstract void BindDataToControls(); public abstract void SaveControlsToData(); #region IKeywordProvider public List&lt;IKeyword&gt; GetKeywords(string categoryName) { if(!(Page is IKeywordProvider )) throw new InvalidOperationException("Page is not IKeywordProvider"); return ((IKeywordProvider) Page).GetKeywords(categoryName); } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload