English 中文(简体)
这是否是一个共同的问题? 不能确定砖墙
原标题:Is this a covariance problem? Not sure if brick wall

我写过协会。 互联网网页将管理表格。 他们根据以下基类重新计算。

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”,以便阅读/阅读。

然后,我还要在我所有表格子的基调上进行“共同”用户控制。 IMaster 表格

但当用户控制制成页时。 FormData(试图将网页投到) IHasFormData<IMasterForm> 它告诉我,网页是 IHasFormData<IForm Subster>,尽管我对IForm Subclass有限制,但他说也是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

}
最佳回答

我首先要看,我是否能够更简明地重申这一复杂问题。 页: 1 IHasFormData<T>。 您有一个已知执行<代码>的物体。 IHasFormData<IForm Subclass>。 页: 1 IHasFormData<IMasterForm>。 你们知道,参考从IForm Subclass改为IMasterForm。 这失败了。

是的?

如果这是对问题的正确表述,那么,是,这是一个交接的问题。 C第3号并不支持接口的同质。 C第4号,如果你能够向汇编者证明夫妻关系是安全的。

让我向各位简要描述,为什么这可能不安全。 你们有几类 Apple果、 Orange和果树,有着明显的分层关系。 页: 1 IList<Apple>, 您希望加入。 IList<Fruit>。 这种var变在C# 4中不合法,不能合法,因为它不安全。 我们允许这样做。 届时,你可以这样做:

IList<Apple> apples = new List<Apple>();
IList<Fruit> fruits = apples;
fruits.Add(new Orange()); 
// We just put an orange into a list of apples!  
// And now the runtime crashes.

通知问题在于<代码> 清单和编号;T>暴露了一种以T为理由的方法。 为了使汇编者能够允许在您的界面上进行 co变:。 IHasFormData<T>没有把T作为论据。 你们说,通过宣布接口asFormData<out T>,一个 m词意思是“只有出现在产出岗位上”。 然后,汇编者将核实你的要求是正确的,并开始允许夫妇转换。

关于C#4中这一特征的更多信息,见我关于该特征设计的说明档案:

http://blogs.msdn.com/ericfallert/archive/tags/Covariance+and+Contravariance/default.aspx”rel=“noreferer”>http://blogs.msdn.com/ericfallert/archive/tags/Covariance+and+Contravariance/default.aspx

问题回答

4.0之前的C#要求所有对通用型的投放物,以准确匹配类型参数。 4.0 介绍共同和反武装,但你试图做的投影无法在以前的版本中进行。

我的基页与你一样,这就是我如何界定地雷。

public abstract class ViewBasePage<TPresenter, TView> : Page, IView
        where TPresenter : Presenter<TView>
        where TView : IView
{
    protected TPresenter _presenter;

    public TPresenter Presenter
    {
        set
        {
            _presenter = value;
            _presenter.View = (TView) ((IView) this);
        }
}

I think you need to be something like FormData = ((IHasFormData<T>) (IMasterForm )Page)).FormData;





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...