.NET事件的特殊方法(添加/删除/升/其他) [英] .NET events special methods (add/remove/raise/other)

查看:198
本文介绍了.NET事件的特殊方法(添加/删除/升/其他)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的<一href="http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.getraisemethod.aspx"><$c$c>EventInfo.GetRaiseMethod和<一href="http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.getothermethods.aspx"><$c$c>EventInfo.GetOtherMethods的方法。显然,CLR支持4种与事件相关的方法:添加,删除,提高和其他。但在C#创建活动只有的添加删除的...我认为的提高的是在VB中使用,因为你必须指定一个的RaiseEvent 方法,当你声明一个自定义事件,但显然并非如此: GetRaiseMethod 总是返回null

I was wondering about the EventInfo.GetRaiseMethod and EventInfo.GetOtherMethods methods. Apparently, the CLR supports 4 kinds of methods associated with events: add, remove, raise, and "others". But events created in C# only have add and remove... I assumed that raise was used in VB, since you have to specify a RaiseEvent method when you declare a custom event, but apparently it's not the case: GetRaiseMethod always returns null.

那么,有没有人知道:

  • 什么是在具有的提高的与事件相关的方法,如果它从来没有使用过的呢?是否有一个具体的MSIL指令,以提高使用这种方法的事件? (我couln't觉得这样的事情在运codeS)
  • 什么是其他方法返回(当然,的不是的返回实际上)通过 GetOtherMethods ?什么是他们应该做的?
  • 有没有实现这些特殊的方法,在首创置业类型?
  • what's the point in having a raise method associated with an event if it's never used? Is there a specific MSIL instruction to raise an event using this method? (I couln't find anything like it in the opcodes)
  • what are the "other" methods returned (well, not returned actually) by GetOtherMethods? What's are they supposed to do?
  • are there types in the BCL that implement those special methods?

推荐答案

据我所知,提高没有太大的使用,我几乎从来没有见过它用。 C ++ / CLI是pretty的多,我知道,可以很容易地宣布加薪方法的唯一语言。看到这个code,例如:

As far as I know, raise isn't used much, and I've practically never seen it used. C++/CLI is pretty much the only language I know that make it easy to declare a raise method. See this code for example:

using namespace System;

ref class Foo {

private:
    Action ^bar;

public:
    event Action ^Bar {
        void add (Action ^action)
        {
            Console::WriteLine ("add");
            bar += action;
        }

        void remove (Action ^action)
        {
            Console::WriteLine ("remove");
            bar -= action;
        }

        void raise ()
        {
            Console::WriteLine ("raise");

            if (!bar)
                return;

            Console::WriteLine ("raise for real");
            bar->Invoke ();
        }
    };
};

void hello ()
{
    Console::WriteLine ("hello");
}

void main ()
{
    Foo ^foo = gcnew Foo ();
    foo->Bar ();

    foo->Bar += gcnew Action (&hello);

    foo->Bar ();
}

其中,正在运行的时候,自然输出:

Which, when being run, naturally outputs:

C:\tmp>test
raise
add
raise
raise for real
hello

要回答你的问题,有没有OP code调用的事件时,编译器将只是发出调用Raise方法:

To answer your question, there's no opcode to invoke an event, the compiler will just emit a call to the raise method:

  IL_0020:  ldloc.0
  IL_0021:  call       instance void Foo::raise_Bar()

就像它发出一个呼吁add_Bar。

Just like it emits a call to add_Bar.

这也是没有价值,作为C#允许你在声明成员的事件类型的范围只调用一个事件,你不能让C#code调用加薪的方法。所以,不,你不会找到这样的BCL中暴露的方法。

It's also worth nothing that as C# allows you to invoke an event exclusively in the scope of the type which declares the member event, you can't get C# code to call that raise method. So no, you won't find such a method exposed in the BCL.

对于。其他样的方法,我从来没有见过任何连接到事件。而href="http://stackoverflow.com/questions/1840229/the-curiosity-of-the-let-property-method">看见他们使用一次性能我只插件,也不是 removeon ,也不是募集的方法,但他们将是事件的一部分,应该在语言需要EX preSS的。在此期间,以发射一个唯一的方法是使用ILASM

As for the .other kind of methods, I've never seen any attached to an event. And I only saw them used once for properties, and neither the book «Inside IL assembler» nor «The CLI annotated standard» give any information about them. But basically, they allow you to attach methods to a property or to a event to bind them semantically. They're neither a addon, nor a removeon, nor a raise method, but they would be part of the event, should a language need to express that. In the meantime, the only way to emit one is to use ilasm.

这篇关于.NET事件的特殊方法(添加/删除/升/其他)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆