【发布时间】:2016-01-11 19:08:20
【问题描述】:
我有这些课程:
class Foo<T1, T2> : Form
where T1, T2 : EventArgs
class MiddleGoo : Foo<X,Y>
class Goo : MiddleGoo
X,Y 只是从 EventArgs 派生的简单类。
我在设计器中看到了 Goo,但我想在 Foo 和 Goo 之间创建一个 Boo 类,如下所示:
class Boo<T1, Y> : Foo<T1, Y>
where T1 : EventArgs
class MiddleGoo : Boo<X,Y>
class Goo : MiddleGoo
中产阶级的解决方法不起作用,有什么想法吗?
编辑:我的意思是 Y 和 X 是像 YEventArgs 和 XEventArgs 这样的类,我的问题是当我将 Y 定义为 T2 时在设计器类 Boo 中看到,但仍然希望通过 T1 保持它的通用性。
EDIT2:我刚刚意识到我拼错了关于 Y 类的一些内容...
public class Foo<T1, T2> : Form
where T1 : EventArgs
where T2 : EventArgs
{
}
public class Boo<T1> : Foo<T1, MyEventArgs2>
where T1 : EventArgs
{
}
public class MiddleGoo : Boo<MyEventArgs1>
{
}
class Goo : MiddleGoo
{
}
public class MyEventArgs2 : EventArgs
{
}
public class MyEventArgs1 : EventArgs
{
}
需要明确的是,我只是在 Designer 中看不到 Boo...(我也看不到 MiddleGoo,但我不需要)
【问题讨论】:
-
“不起作用” - 为什么?什么错误?
-
假设
Foo的声明没有改变,第二段代码肯定是无效的,因为Y不是继承自EventArgs。那是你的“中间”课程吗?这看起来像一个 X-Y 问题。你想做什么? Winforms 设计者不喜欢 forms,更不用说继承具有通用参数的表单了。 -
@stuartd 使用带有通用参数的表单有很多好处,但不幸的是设计师不支持它们,我在一个非常大的项目中使用带有通用参数的表单使用我描述的技巧here 和我有设计师支持,希望你看看并发现它有帮助:)
-
设计者需要创建一个基类的实例来完成它的工作,最明显的方面是所见即所得的外观。当基类是泛型时,这当然是不可能的,它不可能猜测选择哪个特定类型的参数。你可以让这个类在代码中工作,但你必须不使用点击视图。
-
class Boo<T1, Y> : Foo<T1, Y> where T1 : EventArgs也只有加上Y:EventArgs才有效,这里Y是一个通用参数名,不是你说的Y继承自EventArgs
标签: c# .net winforms generics windows-forms-designer