C#:将派生类作为一个泛型参数传递 [英] C#:Passing derived classes as one generic parameter
问题描述
我最近开始更多地了解事件/委托以及课程的扩展.
I recently started learning more about events/delegates in conjunction with extensions to a class.
我想通过向 Windows 窗体控件添加一个名为 SetDraggable()
的扩展方法来将我学到的知识付诸实践,该方法又使用了一个 MouseDown
和 MouseMove
事件来移动控件.
I wanted to put what I learned into practice by adding an extension method to Windows Form controls called SetDraggable()
, which in turn uses a MouseDown
and MouseMove
events to move the control around.
一切正常,除了它只适用于特定控件的想法——在我的例子中,是一个 Button
.
All works fine, except for the idea that it only applies to a specific control -- in my case, a Button
.
namespace Form_Extensions
{
public static class Extensions
{
private static System.Windows.Forms.Button StubButton;
private static Point MouseDownLocation;
public static void SetDraggable(this System.Windows.Forms.Button b)
{
b.MouseDown += b_MouseDown;
b.MouseMove += b_MouseMove;
StubButton = b;
}
private static void b_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
static void b_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
StubButton.Left = e.X + StubButton.Left - MouseDownLocation.X;
StubButton.Top = e.Y + StubButton.Top - MouseDownLocation.Y;
}
}
}
}
可以看出,我需要特定的控件来调用鼠标事件——我无法从 父 类 System.Windows.Forms
访问这些事件.
As can be seen, I need the specific control in order to call the Mouse events -- I cannot access those events from the parent class System.Windows.Forms
.
所以我的问题仍然存在 -- 是否有一个概念允许程序员一般地将所有派生类作为参数传递.我基本上是试图避免为每个控件复制粘贴以下代码,并希望将其概括为从 System.Windows.Forms
派生的所有类.
So my question remains -- is there a concept that allows programmers to generically pass all derived classes as an argument. I am basically trying to avoid copy pasting the below code for every single control and would like to generalize it for all classes derived from System.Windows.Forms
.
据我所知,这个想法的主要缺陷是我假设所有派生类都会有我需要的事件;然而,由于委托形式的函数也存在类似的情况,我希望有人可以权衡涉及对象或参数的情况.
As far as I can tell, the main flaw in that idea is the fact that I am ASSUMING that all derived classes will have the event I need; however, since a similar thing exists for functions in the form of Delegates, I was hoping someone could weigh in for cases involving objects or parameters.
推荐答案
父类不是 System.Windows.Forms
,那只是命名空间.实际的父类是 Control
,你当然可以使用它:) 使用泛型方法也是可能的,但不是真正必要的.
The parent class isn't System.Windows.Forms
, that's just the namespace. The actual parent class is Control
, and you can certainly use that :) Using a generic method is also possible, but not really necessary.
理想情况下,您还希望避免使用那些静态字段,因为可能有多个并发可拖动对象;SetControlDraggable
方法中的闭包效果会更好:
Ideally, you'd also want to avoid those static fields, since it's possible to have multiple concurrent draggables; a closure in the SetControlDraggable
method will work a lot better:
public static void SetControlDraggable(this Control control)
{
Point mouseDownLocation = Point.Empty;
control.MouseDown += (s, e) =>
{
if (e.Button == MouseButtons.Left) mouseDownLocation = e.Location;
}
control.MouseUp += (s, e) =>
{
if (e.Button == MouseButtons.Left)
{
control.Left = e.X + control.Left - mouseDownLocation.X;
control.Top = e.Y + control.Top - mouseDownLocation.Y;
}
}
}
这篇关于C#:将派生类作为一个泛型参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!