Answer
Fifth chapter: Agent
The proxy realizes the pointer function of language like C + +, unlike the function pointer, the proxy is an object-oriented, security type. Proxies are derived from a reference type of a common base class (System) that is pressed into an agent for instance methods known as the constituent entities of an instance or a method of an instance, while a static method is called the constituent entity or class method of a class. The powerful function of an agent is that it can automatically match the method regardless of its type.
Writing an agent consists of three steps:
Write proxy, instantiate, call.
The proxy's declaration uses the following syntax:
delegate void SimpleDelegate ();
Instantiating an agent
Class Test
{
static void F () {
System.Console.WriteLine ("Hello World");
}
static void Main () {
SimpleDelegate d = new SimpleDelegate (F);//press method into
D ();/through proxy;
F ();//not through proxy;
}
}
Finally let's call her
void MultiCall (simpledelegate d, int count) {
for (int i = 0; i < count; i++)
D ();
}
}
We can see that the invocation of the method is done through the proxy, and that the call does not need to know the type of her being invoked. The agent seems to me like an object. She does not call this method directly, but calls her through a middleman.
The following is a detailed description of the power of the agent: first let's implement one such function, consider how to call a subclass's member function with an object that points to the base class. Programmers here are not a bit nostalgic pointers, but in C # such functions can be fully implemented, using a separate agent we can complete this function. The following code comes from the Timothy A. Vanover article.
Namespace Delegatescs
{
Using System;
public class Wisdom//containing proxy classes
{
Public delegate string Giveadvice ();
public string Offeradvice (Giveadvice Words)
{
return Words ();
}
}
public class Parent//base class
{
Public virtual string Advice ()
{
Return ("Listen to Reason");
}
~parent () {}
}
public class Dad:parent//Subclass
{
Public Dad () {}
public override string Advice ()
{
Return ("Listen to Your Mom");
}
~dad () {}
}
public class Mom:parent//Subclass
{
Public Mom () {}
public override string Advice ()
{
Return ("Listen to Your Dad");
}
~mom () {}
}
public class daughter//Not inheriting classes with base classes
{
Public daughter () {}
public string Advice ()
{
Return ("I know all There are to life");
}
~daughter () {}
}
public class Test
{
public static string Calladvice (Parent p)//Use base class
{
Wisdom parents = new Wisdom ();
Wisdom.giveadvice teenagegirls = new Wisdom.giveadvice (p.advice);//Delegate Advice method to Teenagegirls delegate object
Return (parents. Offeradvice (Teenagegirls));
}
public static void Main ()
{
Dad d = new Dad ();
Mom m = new mom ();
Daughter G = new daughter ();
The following two classes are for the base class
Console.WriteLine (Calladvice (d));
Console.WriteLine (Calladvice (m));
The following is a class that is not in the base class, if the call will be wrong.
Console.WriteLine (Calladvice (g));
}
}
}
Agent Two
1〉 Events
The previous chapter explains the basic application of agents, and this chapter will continue to explain the use of in-depth agents. Here we explain using proxies to handle events. A detailed explanation of the event is in another chapter. Handling events is smarter than C + + and VB in C #, you can write an agent and write an event handler, an event handler is a public event that defines overloads in controls and form classes. We will see the application of the agent in the event in the following example.
1. Write Agent
I want to handle the mouse click event and the left or right mouse button to handle some code. Write the following code in your initial control function.
This. MouseDown + = new System.WinForms.MouseEventHandler (this. Form_mousedown);
2. Writing Events
Now you can write event handling, and the output parameters of your event will return the details of the form's mouse event parameters. Mouse Event parameter members below
MouseEventArgs members
button indicates which key is pressed, divided into left, right, middle, and none.
Clicks indicates the number of mouse presses and the release status.
Delta indicates the number of mouse rotation counts
x mouse click X coordinate point
y mouse click y coordinate point
Event Handler
private void Form_mousedown (object sender, System.WinForms.MouseEventArgs e)
{
Switch (E.button)
{
Case MouseButtons.Left:
MessageBox.Show (This, the "left button click");
Break
Case Mousebuttons.right:
MessageBox.Show (this, ' right button click ');
Break
Case Mousebuttons.middle:
Break
Default
Break
}
}
Test your program in your WinForm and you will see that the agent event is associated.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Class I
{
Public I () {}
~i () {}
public void Idoloveyou ()
{System.Console.WriteLine ("I do Love You");}
public void Why () {System.Console.WriteLine ("why?");}
}
Class her
{
Public her () {}
~her () {}
public void IDo ()
{System.Console.WriteLine ("..... ..."); ".}
public void Slient () {System.Console.WriteLine ("...");}
}
Class Telephone
{public delegate void Heartchat ();
Public telephone () {}
~telephone () {}
public void Hello () {System.Console.WriteLine ("Yesterday night,i telephone to my Girlfriend");
}
Class chat{
static void Main () {
I i=new I ();
Her her=new her ();
Telephone Telephone =new telephone ();
Telephone.hello ();
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