整数的自定义事件
我知道这听起来有多愚蠢,但是有没有办法将事件附加到整数上?
例如,如果您处于 while 循环中并且执行: i++;是否有可能做这样的事情?:
int i;
Form1_Load(object sender, EventArgs e)
{
i.MaximumNumberReached += new IntegerMaximumNumberReachedEventArgs();
}
while(x != y)
{
i++
}
private void MaximumNumberReached(object sender, EventAgrs e)
{
if(e.Value == 7)
{
this.Dispose(true);
}
}
或者我应该停止过多地发挥我的想象力吗?
谢谢
I know how silly this may sound, but is there a way to attach an event to an integer?
For example, if you're in a while loop and you do: i++; is it at all possible to do something like this?:
int i;
Form1_Load(object sender, EventArgs e)
{
i.MaximumNumberReached += new IntegerMaximumNumberReachedEventArgs();
}
while(x != y)
{
i++
}
private void MaximumNumberReached(object sender, EventAgrs e)
{
if(e.Value == 7)
{
this.Dispose(true);
}
}
Or should I just stop using my imagination so much?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,不,你不能这样做。如果你有自己的类来表示数字并重载相关运算符,以便它们启动某些事件,那么你可以,但你不能发明事件并将它们附加到现有的类(除了使用 AOP 框架并挂钩)某些方法)。
其次,不,不要停止发挥你的想象力。您可能对以下文章感兴趣:算术溢出检查。
Firstly, no, you can't do that. You could if, say, you had your own class representing numbers and overloaded relevant operators such that they initiated certain events, but you can't invent events and attach them to existing classes (aside, say, from using an AOP framework and hooking into certain methods).
Secondly, no, don't stop using your imagination. You may be interested in the following article: Arithmetic Overflow Checking.
您无法将事件添加到您无法控制的类型中,并且结构上的事件通常是一个非常糟糕的主意;但是:这里最好的方法是使用属性:
You can't add events to a type outside of your control, and events on structs is usually a very bad idea; but: the best approach here would be to use a property:
扩展方法?不是事件,但你可以做类似的事情。 http://msdn.microsoft.com/en-us/library/bb383977.aspx
Extension methods? Not events but you can do something similar. http://msdn.microsoft.com/en-us/library/bb383977.aspx