简体   繁体   English

这个C#构造在做什么?

[英]What is this C# construct doing?

I have seen on another post this and its confusing me... 我在另一篇文章中看到过这个令我困惑的事......

public class MyClass : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;  

   protected void NotifyPropertyChanged(String info)
   {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
   }  

   public string MyProperty
   {
       set
       {
           if (_myProperty != value)
           {
               _myProperty = value;
               NotifyPropertyChanged("MyProperty");
           }
       }
   }
}

MyClass myClass = new MyClass();

myClass.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
{
     actual = e.PropertyName;
};

I'm wondering about the last few lines are doing to be honest, why would the user be assiging a delegate to an event? 我想知道最后几行是不是说实话,为什么用户会委托一个代表参加一个活动呢? Would't they assign a method to it (as an event handler) or even an anonymous method as the event handler? 他们不会为它(作为事件处理程序)或甚至作为事件处理程序的匿名方法分配方法吗?

I thought that events were meant to encapsulate delegates.....? 我认为事件是为了封装代表.....?

You always subscribe to an event (or unsubscribe from it) using a delegate. 始终使用委托订阅事件(或取消订阅)。 Even if you do: 即使你这样做:

button.Click += HandleButtonClick;

that's equivalent to 这相当于

button.Click += new EventHandler(HandleButtonClick);

When you say: 当你说:

Would't they assign a method to it (as an event handler) or even an anonymous method as the event handler? 他们不会为它(作为事件处理程序)或甚至作为事件处理程序的匿名方法分配方法吗?

That's exactly what the last few lines of code do . 这正是最后几行代码所做的 That's what delegate (...) { ... } is. 这就是delegate (...) { ... }的含义。

I thought that events were meant to encapsulate delegates.....? 我认为事件是为了封装代表.....?

Events provide an implementation of the observer pattern, using delegates as the observers. 事件提供了观察者模式的实现,使用委托作为观察者。

Description 描述

This class implements the INotifyPropertyChanged interface 该类实现了INotifyPropertyChanged接口

MSDN Notifies clients that a property value has changed. MSDN通知客户端属性值已更改。

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. INotifyPropertyChanged接口用于通知客户端(通常是绑定客户端)属性值已更改。

This is used for example on controls like the Datagrid. 这用于例如Datagrid之类的控件。 It signals the control that the property has changed and the control should rerender. 它标志着控制权属性已经改变,控制权应该重新出现。

About the Event 关于活动

You always subscribe to an event. 你总是订阅一个活动。

MyClass myClass = new MyClass();
myClass.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
{
     actual = e.PropertyName;
};

is doing the same as 是这样做的

MyClass myClass = new MyClass();
myClass.PropertyChanged += new PropertyChangedEventHandler(myClass_PropertyChanged);

void myClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
     actual = e.PropertyName;     
}

or 要么

MyClass myClass = new MyClass();
myClass.PropertyChanged += myClass_PropertyChanged;

void myClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
     actual = e.PropertyName;    
}

More Information 更多信息

The += adds an anonymous delegate to the event. +=为事件添加匿名委托 Instead of creating a named method with the signature object sender, PropertyChangedEventArgs e , you can use C#2.0 syntax to create such delegates anonymously in the body of another function. 不是使用签名object sender, PropertyChangedEventArgs e创建命名方法,而是可以使用C#2.0语法在另一个函数的主体中匿名创建此类委托。 Another way of doing it would be to use a more concise lambda syntax from C#3.5+: 另一种方法是使用C#3.5 +中更简洁的lambda语法:

myClass.PropertyChanged += (sender, e) { actual = e.PropertyName; };

This syntax was introduced in C# 2.0 They are using an anonymous method here, rather than having to create an actual instance method of the class. 这种语法是在C#2.0中引入的。它们在这里使用匿名方法,而不是必须创建类的实际实例方法。 It's generally considered cleaner. 它通常被认为更清洁。

In C# 3 and above, a Lambda expression could have been used as well. 在C#3及更高版本中,也可以使用Lambda表达式。

They are not assigning a delegate to an event, they are adding a subscriber to that event using an anonymous method. 他们没有为一个事件分配一个委托,他们正在使用匿名方法向该事件添加一个订阅者。

Also, as an aside, the NotifyPropertyChanged method should be changed to: 另外,另外,NotifyPropertyChanged方法应更改为:

protected void NotifyPropertyChanged(String info)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(info));
    }
}

Since there is a potential race condition between the null check and invocation of the delegate. 由于在null检查和委托的调用之间存在潜在的竞争条件。

Technically, you are correct; 从技术上讲,你是对的; events encapsulate delegates. 事件封装代表。 However, event handlers themselves are delegates. 但是,事件处理程序本身就是委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM