C # advanced programming (4) -- Method Group conversion in proxy

Source: Internet
Author: User

The Method Group feature was mentioned in the previous proxy object initialization. The C # compiler can implicitly convert the method group to its compatible proxy object. For example:
[Csharp]
Feedback fbInstance = FeedbackToFile;

Method group is the name of a group of methods. It is called a method group because the method has the feature of overloading and each method name may have several signature values. For example:
[Csharp]
Void MyMethod ()
Void MyMethod (object sender, EventArgs e)

You can use MyMethod as the method group to assign values to the ThreadStart proxy object or EventHandler proxy object.
[Csharp]
ThreadStart x = MyMethod;
EventHandler y = MyMethod;

However, you cannot pass MyMethod as a parameter to an overloaded function that accepts ThreadStart and EventHandler as a parameter. The compiler will report an error that is ambiguous.
Similarly, you cannot implicitly convert a method group to an object of the System. Delegate type, because the compiler does not know which seed proxy type to use to create a proxy object. The solution is to use explicit type conversion.
[Csharp]
Delegate invalid = SomeMethod;
Delegate valid = (ThreadStart) SomeMethod;

In addition, the Method group can be used through the wrapper. For example, Control. Invoke only accepts the System. Delegate type as the parameter. Therefore, there are several ways to use Method group.
[Csharp]
Static void SimpleInvoke (Control control Control,
MethodInvoker invoker)
{Www.2cto.com
Control. Invoke (invoker );
}
...
SimpleInvoke (form, UpdateUI );
Form. Invoke (MethodInvoker) UpdateUI );
MethodInvoker invoker = UpdateUI;
Form. Invoke (invoker );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.