Visual Studio设计器中的抽象通用UserControl继承 [英] Abstract generic UserControl inheritance in Visual Studio designer

查看:164
本文介绍了Visual Studio设计器中的抽象通用UserControl继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个项目中,我正在使用抽象的UserControl.为了能够在Visual Studio中设计此控件,我使用了此答案中提出的代码.现在,我想将其与另一个通用的抽象UserControl一起使用.但是如果我这样做

In one of my projects I'm using an abstract UserControl. To be able to design this control in Visual Studio I'm using the code proposed in this answer. Now I want to use this with another abstract UserControl which is also generic. But if I do

[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<MyBaseControl<T>, UserControl>))]

我遇到了编译器错误

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

CS0416: an attribute argument cannot use type parameters

删除type参数显然也不会编译.

Removing the type parameter obviously doesn't compile either.

我不能从非通用基类派生MyBaseControl,因为它已经从通用基类派生了,所以我尝试用接口修饰它并像这样使用它:

I can't derive MyBaseControl from a non generic base class because it already derives from an generic base class, so I tried decorating it with an interface and use it like so:

[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<IMyBaseControl, UserControl>))]

可以进行编译,但是当我打开设计视图时,我的控件没有被渲染,而是出现了错误

This does compile but when I open the the design view my control doesn't get rendered, instead I get the error

提供的泛型参数的数量不等于泛型类型定义的灵巧性.

The number of generic arguments provided doesn't equal the arity of the generic type definition.

有没有办法解决这个问题?

Is there a way to solve this?

推荐答案

我想您有一个控件AbstractGenericBase<T> : GenericBase<T>,其中GenericBase<T>是具有以下定义的控件:GenericBase<T>: UserControl.

I suppose you have a control AbstractGenericBase<T> : GenericBase<T> which GenericBase<T> is a control having such definition: GenericBase<T>: UserControl.

因此,如果要在设计器中显示AbstractGenericBase<T>,可以使用以下代码:

So if you want to show AbstractGenericBase<T> in designer, you can use this code:

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

注意

  • 目标是在设计器中显示此类:
    public abstract partial class AbstractGenericBase<T> : GenericBase<T>
  • 如果T具有某些类型约束,请使用Dummy来满足一般约束,而不是GenericBase<object>中的object.
  • 该解决方案基于以下事实:当设计人员希望在设计图面上显示您的控件时,它将尝试创建控件的基类的实例.当控件的基类是泛型类时,设计人员将不知道如何创建基类的实例.
    在上述解决方案中,为了提供设计时支持,我们曾向设计人员说过,控件的基础是NonGenericBase,但在运行时,我们将GenericBase<T>保留为控件的基类.
  • The goal is showing this class in designer:
    public abstract partial class AbstractGenericBase<T> : GenericBase<T>
  • If T has some type constraints, instead of object in GenericBase<object> use a Dummy which satisfies the generic constraints.
  • The solution is based on this fact: When the designer wants to show your control in design surface, it tries to create an instance of base class of your control. When the base class of your control is a generic class the designer doesn't know how to create an instance of base class.
    In above solution, for design-time support, we said to designer the base for our control is NonGenericBase, but for run-time, we keep GenericBase<T> as base class of our control.

这篇关于Visual Studio设计器中的抽象通用UserControl继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆