我正在构建一个扩展 DataGridView 的 WinForms 自定义控件。
interface IMyControl<A, B> { }
public partial class MyControl<A, B> : DataGridView, IMyControl<A, B>
{
}
A
和 B
是业务域对象类型。
但是,代码不会编译。 MyControl.Designer.cs
无法编译。
protected override void Dispose(bool disposing) //no suitable method found to override
{
if (disposing && (components != null))
{
components.Dispose();
}
//'object' does not contain a definition for 'Dispose'
base.Dispose(disposing);
}
'Infrastructure.MyControl.Dispose(bool)': no suitable method found to override MyControl.Designer.cs
最佳答案
你不能有通用控件。
试试这个:
interface IMyControl<A, B> { }
public partial abstract class MyControlBase<A, B> : DataGridView, IMyControl<A, B>
{
// Generic code goes here
}
// Create non-generic wrappers for the generic base class
public partial class MyControl_One : DataGridView, MyControlBase<SomeType, OtherType>
{
// Type-specific (if any) code goes here
}
public partial class MyControl_Two : DataGridView, MyControlBase<MyType, YourType>
{
// Type-specific (if any) code goes here
}
将您当前拥有的任何通用代码留在通用基类中。包装器类可以非常精简,因为它们仅用于提供非通用控件以添加到您的表单。
关于c# - 带有类型参数的自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12790636/