Analyze problems
Before analyzing the internal structure of the Delegate, let's review the most basic concepts. In C #, what are the differences between static and instance methods. Similar to most other advanced object-oriented languages, static methods are defined by the keyword static in C #. Static methods can be accessed by class names without any instance objects, in a static method, no non-static members of the access type are allowed. The instance method needs to be called through a specific instance object to access any member of the Instance Object.
Now let's take a look at the differences between the delegated instance binding method and the static method. As mentioned above, when an instance method is called, it needs to be accessed through the instance object. As you can imagine, to bind an instance method to a delegate, you must obtain the code segment of the instance method and the information of the instance object at the same time, so that when the delegate is called back.. net. Shows the internal structure of the delegate.
_ Target is a reference to the target instance. When an instance method is bound to a delegate, this parameter is set to an instance object of the type of the method. When a static method is bound to the delegate, this parameter is set to null.
_ Methodptr is a pointer to the code snippet of the binding method, which is very similar to the function pointer in C ++. Binding static methods or instance methods does not differ in the settings of this Member.
In fact. for the custom delegate type of multicastdelegate, there is another member variable: _ Prev, which points to the next delegate in the delegate chain. The concept of chain delegate will be overwritten in the later lady. So far, the reader has understood the difference between the delegated instance binding method and the static method, that is, the member settings of _ target.
Answer
When the delegate is bound to a static method, the internal object member Variable _ target is set to null. When the delegate is bound to an instance method, _ target will be set to an instance object pointing to the type of the instance method. When the delegate is executed, the instance of this object will be used to call the instance method.
What is the difference between the delegate callback static method and the instance method?