我有一个对象,它在我的类中是私有的。如果该对象触发了一个事件,我希望将该事件传递给正在使用我的类的任何对象。目前我是这样做的,我在我的构造函数中放入:
cbName.CheckedChanged += ((sender, args) => this.CheckChanged(this,args));有没有更好的方法来做到这一点,有没有像类不会被释放这样的陷阱,因为它本身有一个事件,我需要在dispose函数中手动取消订阅?
将发送方从触发对象更改为this是可选的。
测试代码的完整版本
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace placeholder
{
internal class FilterBase : UserControl, IFilterObject
{
public FilterBase(string name)
{
InitializeComponent();
cbName.CheckedChanged += ((sender, args) => this.CheckChanged(this,args));
cbName.Name = name;
this.Name = name;
}
private CheckBox cbName;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cbName = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// cbName
//
this.cbName.AutoSize = true;
this.cbName.Location = new System.Drawing.Point(4, 4);
this.cbName.Name = "cbName";
this.cbName.Size = new System.Drawing.Size(79, 17);
this.cbName.TabIndex = 0;
this.cbName.Text = "Filter Name";
this.cbName.UseVisualStyleBackColor = true;
//
// UserControl1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.Controls.Add(this.cbName);
this.Name = "Filter Name";
this.Size = new System.Drawing.Size(86, 24);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public event EventHandler CheckChanged;
public bool Checked
{
get { return cbName.Checked; }
}
}
}发布于 2010-07-23 01:00:17
event实际上类似于自动属性。您可以定义自己的add和remove方法,将委托直接传递给子控件,从而消除额外级别的间接性:
public event EventHandler CheckChanged {
add { cbName.CheckChanged += value; }
remove { cbName.CheckChanged -= value; }
}这将删除存储在类中的额外Delegate (因为Delegate字段在标准事件的幕后使用)
发布于 2010-07-23 00:54:57
嗯,这将防止当前对象在cbName活动时被垃圾回收,但听起来这不太可能是一个问题……这是固有的事实,无论您做什么(因为您想要触发该对象的CheckChanged处理程序),都会获得对this的引用。
这样做的一个缺点是,即使你想取消订阅,也不能取消订阅(例如,在Dispose方法中)。另一种方法是使用实际的方法:
private void CbNameCheckedChangedHandler(object sender, EventArgs e)
{
CheckedChanged(this, args);
}
...
cbName.CheckedChanged += CbNameCheckedChangedHandler;
// If you ever want to remove the handler
cbName.CheckedChanged -= CbNameCheckedChangedHandler;请注意,这都是假设您的CheckedChanged事件是空安全的,例如,它被声明为:
public event EventHandler CheckedChanged = delegate {};否则,如果没有人订阅该事件,当cbName.CheckedChanged触发时,您将获得一个NullReferenceException。
https://stackoverflow.com/questions/3311271
复制相似问题