How is C # event handling implemented?

When you update DataGridView.DataSource

using an assignment statement, the event handler is called DataSourceChanged

. You cannot overload the assignment of the op in C # since the event handler gets called internally?

public void foo(){
    this.dataGridView.DataSourceChanged += new EventHandler(bar);
    this.dataGridView.DataSource = dt;
}

//this handler is called after datasource changes
private void bar(object sender, EventArgs e) {}

      

+3


source to share


5 answers


DataSource

implemented as a property.

private object _dataSource;
public object DataSource {
    get
    {
        return _dataSource;
    }
    set
    {
        if (value != _dataSource) {
            _dataSource = value;
            UpdateTheDataGridView();
            RaiseTheDataSourceChangedEvent();
        }
    }
}

      



The read / write property is a set of two methods that are automatically called when the property is accessed; set

when you assign a value get

, when you read a property.

+6


source


It is not entirely clear what you are asking. If you're asking how a property setter can do anything: a property setter is a method. It works like any other method. In this case, when the setter property is called, it checks if it needs to raise the event, and if it does, then it raises the event.

If you're asking how this event is implemented, John's article is certainly great. If you're looking for additional technical analysis of how events are implemented in C # and how the implementation has changed over time, see Chris Burroughs' blog articles on the topic:

http://blogs.msdn.com/b/cburrows/archive/tags/events/



Start at the bottom.

Chris was a developer who designed and implemented many changes to compiler semantic analysis and code generation.

+6


source


Pre-event is exactly what it does, but I think of events as properties - except instead of getter and setter, you have actions add

and remove

, each of which accepts a delegate to subscribe or unsubscribe to / from the event.

A field-like event in C # implements an event using a field for a delegate, and fixed implementations add / remove is a bit like an auto-implemented property. But you can do whatever you like in the handler add

or remove

.

For more information, see the article on delegates and events .

As for the data source itself - it's not a field assignment - it assigns to a property, that is, again, which can do whatever it wants, including raising the event DataSourceChanged

.

+5


source


Datasource is a property and an event handler is called on the collection.

You can use ilspy to see how this is implemented.

0


source


The event handler is called internally because the class providing the event, in this case, DataGridView

explicitly fires the event at some point within its code. The set property for DataSource

sounds like a good place to fire this particular event, although I haven't looked at the source code to check for sure.

I would suggest looking for a simple tutorial on how to provide a custom event for another class. This will give you a better idea of ​​how the event provider is supposed to work without too much detail.

-1


source







All Articles