C# generics usercontrol

后端 未结 7 1114
梦如初夏
梦如初夏 2020-12-03 05:10

I would like to define the following control:

public partial class ObjectSelectorControl : UserControl where T : class 

The proble

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 05:24

    There are some restrictions on what your control can or cannot do in order to be able to use the designer. Fundamentally they all revolve around the designer being able to instantiate your class (must have a parameterless constructor, can't be abstract, etc.). Because the designer has no idea what type to pass as a generic argument (and I doubt this is even a consideration), your class can't be instantiated.

    Your best hope would be to create your UserControl and change the constructor to protected (this, I believe, will work, since the designer uses reflection and ignores visibility, but I'm not 100% positive). You can then inherit from that UserControl and create your generic class and call the base (protected) constructor.

提交回复
热议问题