Use reflection to distribute Switch operations

xiaoxiao2021-03-06  64

I saw a very useful thing today, plus anything in my blog, so I copied it.

Original address:

Point clear of the studio:

Liang Lifeng

Sometimes, we often need to deal with some big Switch statements, such as:

Public Interface IACTION

{

void doaction; "String ActionName);

}

Public Class OkcanceLAction: IACTION

{

Public void doaction (String ActionName)

{

Switch (actionname)

{

Case "OK":

Console.writeline ("OK Action Raised.");

Break;

Case "Cancel":

Console.writeline ("Cancel Action Raised.");

Break;

DEFAULT:

"" ActionName Not Defeding. ");

}

}

}

In general, if this Switch is relatively large, you can use the following method to distribute:

Public Interface IACTION

{

void doaction; "String ActionName);

}

Public Class OkcanceLAction: IACTION

{

Public void doaction (String ActionName)

{

Switch (actionname)

{

Case "OK":

OK_Clicked ();

Break;

Case "Cancel":

CANCEL_CLICKED ();

Break;

DEFAULT:

"" ActionName Not Defeding. ");

}

}

Private void ok_clicked ()

{

Console.writeline ("OK Action Raised.");

}

Private void Cancel_clicked ()

{

Console.writeline ("Cancel Action Raised.");

}

}

However, after the reflection is used, the above distribution methods can also be further encapsulated to be very simple when the action of the photo is very simple. Of course, reflective or custom attribute combinations to make more accurate customization:

[AttributeUSAG (AttributeTargets.method)]

Public Class ActionMethodAttribute: Attribute: Attribute

{

Public String ActionName;

Public ActionMethodAttribute (String ActionName)

{

THIS.ActionName = actionname;

}

}

In addition, define a custom exception:

Public class actionNameNotDefinedException: Exception

{

Public actionNameNotDefinedException (): Base ("Action Name Unefained") {}

Public actionNameNotDefinedException (String ErrorMessage): BASE (ErrorMessage) {}

}

Then is the most important ActionBase class:

Public Abstract Class ActionBase {

Private hashtable ht = new hashtable ();

// HashTable does not save the order characteristics, so join this ArrayList

Private arraylist al = new arraylist ();

Public string [] getActionNames ()

{

Return (String []) Al.toArray (TypeOf (String));

}

Public actionbase ()

{

Console.writeline (this.tostring ());

TYPE T = this.gettype ();

Foreach (BindingFlags.Declaredonly | BindingFlags.instance | BindingFlags.nonpublic))

{

ActionMethodAttribute [] MIS = (ActionMethodAttribute []) mi.getcustomattributes (TypeOf (ActionMethodAttribute), FALSE

IF (Mis! = Null && mis Mis.length> 0)

{

String actionName = MIS [0] .ArtionName

Al.Add (actionname);

HT.Add (ActionName, Mi);

}

}

}

Public void doaction (String ActionName)

{

IF (ht.contains (actionname))

{

(MethodInfo) HT [ActionName]). Invoke (this, new object [] {});

}

Else

{

Throw new actionnamenotdefinedException ();

}

}

}

Then, the definition of the real Action class is very simple:

Public Class OkcanceLAction: ActionBase

{

[ActionMethod ("OK")]]]

Private void ok_clicked ()

{

Console.writeline ("OK Action Raised.");

}

[ActionMethod ("Cancel")]]]]

Private void Cancel_clicked ()

{

Console.writeline ("Cancel Action Raised.");

}

}

The call is running as follows:

Class ApplicationEntry

{

[Stathread]

Static void main (string [] args)

{

Console.writeLine ("/");

ActionBase a = new okcanceland ();

Console.writeLine ("/");

String [] ss = a.getActionNames ();

Console.writeline (String.Join ("/ R / N", SS);

Console.writeLine ("/");

FOREACH (String S in SS)

{

A.DOACTION (S);

}

Console.writeLine ("/");

Console.readline ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-85475.html

New Post(0)