我有以下的WinForms类:通用构造函数:T entity = new T();
class EntityEditorForm<T>: System.Windows.Forms.Form
where T: ICloneable<T> {}
class EntityCollectionEditorForm<T> : System.Windows.Forms.Form
where T: ICloneable<T> {}
第一种形式类是<T>
,在运行时根据T.
第二种类型创建控制编辑器是一个管理器用于收集<T>
并具有添加,编辑和删除功能。该集合显示在listview控件中,并使用自定义属性通过反射填充字段。
用于添加和编辑按钮的代码如下所示:
private void buttonEdit_Click (object sender, System.EventArgs e)
{
T entity = default(T);
entity = (T) this.listView.SelectedItems[0].Tag;
new EntityEditor<T>(entity).ShowDialog(this);
}
private void buttonEdit_Click (object sender, System.EventArgs e)
{
T entity = new T(); //This is the code which is causing issues
entity = (T) this.listView.SelectedItems[0].Tag;
new EntityEditor<T>(entity).ShowDialog(this);
}
的default(T)
作品编辑的情况下,但我在与添加的场景麻烦。 T entity = new T();
似乎不合法。
很显然,我不知道如何在相同的where子句中添加多个约束。修改声明'类EntityCollectionEditorForm < T >:System.Windows.Forms.Form中,其中T:ICloneable < T >,新的(){}'工作。 – 2012-01-09 09:41:54
我一直在阅读文章,但似乎无法理解约束条件。任何卧铺解释将不胜感激:)。 – 2012-01-09 09:43:37