是有可能使在.NET 3.5中一个通用控制?是有、能使、NET

2023-09-02 01:56:41 作者:丿sa灬羽化恋佳

我有以下一般用户控件声明:

I've got the following Generic usercontrol declared:

public partial class MessageBase<T> : UserControl
    {
        protected T myEntry;
        public MessageBase()
        {
            InitializeComponent();
        }
        public MessageBase(T newEntry)
        {
            InitializeComponent();
            myEntry = newEntry;
        }    
    }
}

不过,编译器不会允许我这样做:

But the compiler won't allow me to do this:

public partial class MessageControl : MessageBase<Post>
{
    public MessageControl()
    {
        InitializeComponent();
    }
}

如何创建在C#泛型的用户控制?

How do I create a generic user control in C#?

推荐答案

试试这个

public partial class MessageControl : MessageControlBase
{    
    public MessageControl()    
    {
        InitializeComponent();    
    }
}

public class MessageControlBase : MessageBase<Post>
{}

的关键,让设计师的工作是该类的您正在编辑的基类绝不能通用的。

The key to getting the designer to work is that the base class of the class you are editing must not be generic.