删除多余的委托构造函数调用?

发布于 2024-11-29 14:46:44 字数 404 浏览 1 评论 0原文

我下载了 ReSharper,它告诉我更改这一行:

dispMap.OnDraw += new EventHandler(dispMap_OnDraw);

更改为这一行:

dispMap.OnDraw += dispMap_OnDraw;

因为第一行是“冗余委托构造函数调用”。

这是真的吗?在自动生成的表单设计器代码中,语法基于第一段代码,当输入 dispMap.OnDraw += 并按 TAB 键时,IDE 会自动生成 new EventHandler(dispMap_OnDraw)< /code>

我只是对这个感到好奇。 ReSharper 有道理吗?

I downloaded ReSharper and it is telling me to change this line:

dispMap.OnDraw += new EventHandler(dispMap_OnDraw);

To be this line:

dispMap.OnDraw += dispMap_OnDraw;

Because the first line is a "redundant delegate constructor call."

Is this true? In the automatically generated designer code for forms the syntax is based on the first piece of code and when typing in dispMap.OnDraw += and hitting TAB the IDE automatically generates new EventHandler(dispMap_OnDraw)

I'm just curious about this one. Does ReSharper have a point?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

梦境 2024-12-06 14:46:44

是的,这是正确的。我在几个案例中都这样做过。

委托构造函数调用应该是隐式的;该类型可以从 OnDraw 推断出来,并根据 dispMap_OnDraw 的方法签名进行验证。

此外,这篇 MSDN 文章 中的引用似乎是相关的:

因为 += 运算符只是连接内部调用
一个委托到另一个委托的列表,您可以使用 += 添加一个
匿名方法。请注意,对于匿名事件处理,您不能
使用 -= 运算符删除事件处理方法,除非
匿名方法被添加为处理程序,首先将其存储到
委托,然后向该事件注册该委托。

我相信无论哪种方式都会创建委托实例,但由于在隐式实例化时没有委托的对象引用,因此无法使用 -= 运算符将其删除。

Yes, this is correct. I have done this in several cases.

The delegate constructor call should be implicit; the type can be inferred from OnDraw and validated against the method signature of dispMap_OnDraw.

Also, a quote from this MSDN article appears relevant:

Because the += operator merely concatenates the internal invocation
list of one delegate to another, you can use the += to add an
anonymous method. Note that with anonymous event handling, you cannot
remove the event handling method using the -= operator unless the
anonymous method was added as a handler by first storing it to a
delegate and then registering that delegate with the event.

I believe the delegate instance is created either way, but since you don't have an object reference for the delegate when you implicitly instantiate, you can't remove it with the -= operator.

┊风居住的梦幻卍 2024-12-06 14:46:44

这确实有道理。第二行是第一行的简写。根据您的编码标准/约定,您可以使用其中任何一个,但第一个确实会增加很多噪音。

It does have a point. The second line is shorthand for the first. Depending on your coding standards/conventions, you could use either one, but the first one does add a lot of noise.

风苍溪 2024-12-06 14:46:44

如果您比较这两种情况下生成的 IL,您会发现它们是相同的。以下是 C# 中的两种情况以及它们产生的 IL。

示例 C#:

namespace EventTest
{
    public class Publisher
    {
        public delegate void SomeEvent(object sender);
        public event SomeEvent OnSomeEvent;
        public event SomeEvent OnOtherEvent;
    }

    public class Subscriber
    {
        public Subscriber(Publisher p)
        {
            p.OnSomeEvent += new Publisher.SomeEvent(Respond);
            p.OnOtherEvent += Respond;
        }

        public void Respond(object sender)
        {

        }
    }
}

这是构造函数的 IL。请注意 IL_000aIL_0028 行。

.method public hidebysig specialname rtspecialname 
        instance void  .ctor(class EventTest.Publisher p) cil managed
{
  // Code size       48 (0x30)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
  IL_0006:  nop
  IL_0007:  nop
  IL_0008:  ldarg.1
  IL_0009:  ldarg.0
  IL_000a:  ldftn      instance void EventTest.Subscriber::Respond(object)
  IL_0010:  newobj     instance void EventTest.Publisher/SomeEvent::.ctor(object,
                                                                          native int)
  IL_0015:  callvirt   instance void EventTest.Publisher::add_OnSomeEvent(class EventTest.Publisher/SomeEvent)
  IL_001a:  nop
  IL_001b:  ldarg.1
  IL_001c:  ldarg.0
  IL_001d:  ldftn      instance void EventTest.Subscriber::Respond(object)
  IL_0023:  newobj     instance void EventTest.Publisher/SomeEvent::.ctor(object,
                                                                          native int)
  IL_0028:  callvirt   instance void EventTest.Publisher::add_OnOtherEvent(class EventTest.Publisher/SomeEvent)
  IL_002d:  nop
  IL_002e:  nop
  IL_002f:  ret
} 

结论:我认为没有任何理由更改您的代码,它们是等效的。

If you compare the IL generated in both cases, you'll see that they are the same. Here's both cases in C#, and the IL they result in.

Example C#:

namespace EventTest
{
    public class Publisher
    {
        public delegate void SomeEvent(object sender);
        public event SomeEvent OnSomeEvent;
        public event SomeEvent OnOtherEvent;
    }

    public class Subscriber
    {
        public Subscriber(Publisher p)
        {
            p.OnSomeEvent += new Publisher.SomeEvent(Respond);
            p.OnOtherEvent += Respond;
        }

        public void Respond(object sender)
        {

        }
    }
}

Here's the IL for the constructor. Pay attention to lines IL_000a through IL_0028.

.method public hidebysig specialname rtspecialname 
        instance void  .ctor(class EventTest.Publisher p) cil managed
{
  // Code size       48 (0x30)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
  IL_0006:  nop
  IL_0007:  nop
  IL_0008:  ldarg.1
  IL_0009:  ldarg.0
  IL_000a:  ldftn      instance void EventTest.Subscriber::Respond(object)
  IL_0010:  newobj     instance void EventTest.Publisher/SomeEvent::.ctor(object,
                                                                          native int)
  IL_0015:  callvirt   instance void EventTest.Publisher::add_OnSomeEvent(class EventTest.Publisher/SomeEvent)
  IL_001a:  nop
  IL_001b:  ldarg.1
  IL_001c:  ldarg.0
  IL_001d:  ldftn      instance void EventTest.Subscriber::Respond(object)
  IL_0023:  newobj     instance void EventTest.Publisher/SomeEvent::.ctor(object,
                                                                          native int)
  IL_0028:  callvirt   instance void EventTest.Publisher::add_OnOtherEvent(class EventTest.Publisher/SomeEvent)
  IL_002d:  nop
  IL_002e:  nop
  IL_002f:  ret
} 

Conclusion: I don't see any reason to change your code, they are equivalent.

叫嚣ゝ 2024-12-06 14:46:44

它工作得很好,我有 DevExpress,它也告诉我同样的事情!

it works fine, I have DevExpress and it tells me the same!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文