Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Visual Studio designer中的抽象泛型用户控件继承_C#_Winforms_Visual Studio_Windows Forms Designer_Designer - Fatal编程技术网

C# Visual Studio designer中的抽象泛型用户控件继承

C# Visual Studio designer中的抽象泛型用户控件继承,c#,winforms,visual-studio,windows-forms-designer,designer,C#,Winforms,Visual Studio,Windows Forms Designer,Designer,在我的一个项目中,我使用了一个抽象用户控件。为了能够在VisualStudio中设计此控件,我正在使用中提出的代码。现在我想将它与另一个抽象UserControl一起使用,它也是通用的。但如果我这样做了 [TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<MyBaseControl<T>, UserControl>))] [TypeDescriptionProvider(typeof(Ab

在我的一个项目中,我使用了一个抽象用户控件。为了能够在VisualStudio中设计此控件,我正在使用中提出的代码。现在我想将它与另一个抽象UserControl一起使用,它也是通用的。但如果我这样做了

[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<MyBaseControl<T>, UserControl>))]
[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider))]
我得到了编译器错误

CS0416:属性参数不能使用类型参数

删除类型参数显然也不会编译

我无法从非泛型基类派生MyBaseControl,因为它已经从泛型基类派生,所以我尝试用接口装饰它,并像这样使用它:

[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<IMyBaseControl, UserControl>))]
[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider))]
这是可以编译的,但是当我打开设计视图时,我的控件没有被渲染,而是出现了错误

提供的泛型参数的数量不等于泛型类型定义的arity


有办法解决这个问题吗?

我想你有一个控件
AbstractGenericBase:GenericBase
,它
GenericBase
是一个定义如下的控件:
GenericBase:UserControl

因此,如果要在designer中显示
AbstractGenericBase
,可以使用以下代码:

using System.ComponentModel;
using System.Windows.Forms;

#if DEBUG
public abstract partial class AbstractGenericBase<T> : NonGenericBase
#else
public partial class AbstractGenericBase<T> : GenericBase<T>
#endif
{
    public AbstractGenericBase()
    {
        InitializeComponent();
    }
}
#if DEBUG
public class NonGenericBase : GenericBase<object> { }
#endif
使用System.ComponentModel;
使用System.Windows.Forms;
#如果调试
公共抽象部分类AbstractGenericBase:NonGenericBase
#否则
公共部分类AbstractGenericBase:GenericBase
#恩迪夫
{
公共抽象通用库()
{
初始化组件();
}
}
#如果调试
公共类非通用库:通用库{}
#恩迪夫
注意


  • 目标是在designer中显示该类:
    公共抽象部分类AbstractGenericBase:GenericBase
  • 如果
    T
    具有某些类型约束,则在
    GenericBase
    中使用满足一般约束的
    Dummy
    ,而不是
    object
  • 解决方案基于以下事实:当设计器希望在设计图面中显示控件时,它会尝试创建控件基类的实例。当控件的基类是泛型类时,设计器不知道如何创建基类的实例。
    在上面的解决方案中,为了设计时支持,我们说要设计控件的基类是
    NonGenericBase
    ,但对于运行时,我们将
    GenericBase
    保留为控件的基类
Arributes与泛型的工作方式不同,因此为什么
[TypeDescriptionProvider(typeof
因为它不能使用泛型目标是在designer中显示此类:
公共抽象部分类AbstractGenericBase:GenericBase
。如果您的目标不同,请告诉我。谢谢,这很有效。我想补充一点,您必须为非泛型类指定泛型类以外的名称,或者将其保留在di中不同的文件。否则会出现错误
类型“”由同一文件中的多个分部类组成