整数的自定义事件

发布于 2024-10-19 13:08:07 字数 424 浏览 4 评论 0原文

我知道这听起来有多愚蠢,但是有没有办法将事件附加到整数上?

例如,如果您处于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小草泠泠 2024-10-26 13:08:07

首先,不,你不能这样做。如果你有自己的类来表示数字并重载相关运算符,以便它们启动某些事件,那么你可以,但你不能发明事件并将它们附加到现有的类(除了使用 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.

恬淡成诗 2024-10-26 13:08:07

您无法将事件添加到您无法控制的类型中,并且结构上的事件通常是一个非常糟糕的主意;但是:这里最好的方法是使用属性:

private int someMeaningfulName;
public int SomeMeaningfulName {
    get { return someMeaningfulName; }
    set {
        // pre-validation checks and "changing" notification
        someMeaningfulName = value;
        // side effect code and "changed" notification
    }
}

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:

private int someMeaningfulName;
public int SomeMeaningfulName {
    get { return someMeaningfulName; }
    set {
        // pre-validation checks and "changing" notification
        someMeaningfulName = value;
        // side effect code and "changed" notification
    }
}
忆依然 2024-10-26 13:08:07

扩展方法?不是事件,但你可以做类似的事情。 http://msdn.microsoft.com/en-us/library/bb383977.aspx

class Program
{
    static void Main(string[] args)
    {
        int a = 10;
        a.MaximumNumberReached();
    }
}

public static class Extns
{
    public static void MaximumNumberReached(this int number)
    {
        if (number == 7)
        {
            //Do something
        }
    }
}

Extension methods? Not events but you can do something similar. http://msdn.microsoft.com/en-us/library/bb383977.aspx

class Program
{
    static void Main(string[] args)
    {
        int a = 10;
        a.MaximumNumberReached();
    }
}

public static class Extns
{
    public static void MaximumNumberReached(this int number)
    {
        if (number == 7)
        {
            //Do something
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文