从泛型类派生的c#问题

从泛型类派生的c#问题,c#,generics,implicit-conversion,C#,Generics,Implicit Conversion,以下是工人阶级: public class CatalogManager<T1, T2, T3> where T1 : CatalogDataEntryForm<DataEntryControl>, new() where T2 : CatalogDataGridForm<DataGridControl>, new()

以下是工人阶级:

public class CatalogManager<T1, T2, T3> where T1 : CatalogDataEntryForm<DataEntryControl>, new()
                                        where T2 : CatalogDataGridForm<DataGridControl>, new()
                                        where T3 : CatalogBusinessObject
{
    public CatalogManager()
    {
        _DataGridFrom = new T2();
        InitGridformToolbarItemeEvents();
    }

}

public class BankDataEntryForm : CatalogDataEntryForm<BankDataEntryControl>
{
}

public class BankDataGridForm : CatalogDataGridForm<BankDataGridControl>
{
}
公共类CatalogManager,其中T1:CatalogDataEntryForm,new()
其中T2:CatalogDataGridForm,new()
其中T3:CatalogBusinessObject
{
公共目录管理器()
{
_DataGridFrom=newt2();
initGridFormToolbarItemEvents();
}
}
公共类BankDataEntryForm:CatalogDataEntryForm
{
}
公共类BankDataGridForm:CatalogDataGridForm
{
}
但是,下面的派生类出现错误:

public class BankManager : CatalogManager<BankDataEntryForm, BankDataGridForm, BankBo>
{
    public BankManager()
    {

    }
}
公共类银行经理:CatalogManager
{
公共银行经理()
{
}
}
错误消息:

错误CS0311类型“BankDataEntryForm”不能用作类型 泛型类型或方法“CatalogManager”中的参数“T1”。错误CS0311类型“BankDataGridForm”不能用作类型 泛型类型或方法“CatalogManager”中的参数“T2”


非常感谢您的帮助。

这是一个问题,斯拉克人说
DataEntryControl
BankDataEntryControl
不同,尽管它们是继承关系

从.NETFramework4开始,VisualBasic和C#都有关键字,可以将接口和委托的泛型类型参数标记为协变或逆变

因此,您可以尝试为这些类创建接口

  • CatalogDataEntryForm
  • CatalogDataGridForm
然后让这些类实现接口

public interface ICatalogDataEntryForm<out T> 
{ }

public interface ICatalogDataGridForm<out T> 
{ }

public class CatalogDataEntryForm<T> : ICatalogDataEntryForm<T>
{ }
public class CatalogDataGridForm<T> : ICatalogDataGridForm<T>
{}
public class CatalogManager<T1, T2, T3> where T1 : ICatalogDataEntryForm<DataEntryControl>, new()
                                       where T2 : ICatalogDataGridForm<DataGridControl>, new()
                                       where T3 : CatalogBusinessObject
{
    public CatalogManager()
    {

    }

}
然后让
CatalogManager
类与这些接口联系起来

public interface ICatalogDataEntryForm<out T> 
{ }

public interface ICatalogDataGridForm<out T> 
{ }

public class CatalogDataEntryForm<T> : ICatalogDataEntryForm<T>
{ }
public class CatalogDataGridForm<T> : ICatalogDataGridForm<T>
{}
public class CatalogManager<T1, T2, T3> where T1 : ICatalogDataEntryForm<DataEntryControl>, new()
                                       where T2 : ICatalogDataGridForm<DataGridControl>, new()
                                       where T3 : CatalogBusinessObject
{
    public CatalogManager()
    {

    }

}
公共类CatalogManager,其中T1:ICatalogDataEntryForm,new()
其中T2:ICatalogDataGridForm,new()
其中T3:CatalogBusinessObject
{
公共目录管理器()
{
}
}

我认为您在头等舱中缺少了一个新的()。
DataEntryControl
BankDataEntryControl
不同。了解协方差。