The interface is as follows:
Then, click the UI after the filter button:
Look, the filter out, the code is as follows (JS can tune C # write the UserInfo class of the various attributes)
Copy Code code as follows:
public class UserInfo
{
public string FirstName {get; set;}
public string LastName {get; set;}
public int Age {get; set;}
public override string ToString ()
{
return string. Format ("{0}, {1}: {2}", FirstName, LastName, age);
}
}
private void btnSearch_Click (object sender, EventArgs e)
{
list<userinfo> users = Filterusers (TxtFilter.Text.Trim ());
Lstusers.datasource = users;
}
Private list<userinfo> Filterusers (string jsfilter)
{
list<userinfo> results = new list<userinfo> ();
if (Jsfilter.trim (). Length = = 0)
return this.users;
Try
{
using (javascriptcontext context = new Javascriptcontext ())
{
foreach (UserInfo user in this.users)
{
Context. Setparameter ("user", user);
Context. Setparameter ("Isinclude", false);
String js = string. Format (@)
if ({0})
Isinclude=true;
Else
Isinclude=false; ", Jsfilter);
Context. Run (JS);
BOOL Isinclude = (bool) context. GetParameter ("Isinclude");
if (isinclude)
Results. ADD (user);
}
}
}
Catch
{
Txtfilter.focus ();
Txtfilter.selectall ();
return this.users;
}
return results;
}
Let's take a look at efficiency:
Copy Code code as follows:
private void btnTest_Click (object sender, EventArgs e)
{
using (javascriptcontext context = new Javascriptcontext ())
{
stopwatch SW = new Stopwatch ();
Sw. Start ();
int i=1000000;
while (i--> 0)
{
Context. Setparameter ("II", 1);
Context. Setparameter ("JJ", 2);
Context. Setparameter ("sum", 0);
String js = string. Format (@ "SUM=II+JJ;");
Context. Run (JS);
int sum = (int) context. GetParameter ("sum");
}
Sw. Stop ();
Long usejs_total = SW. Elapsedmilliseconds;
float Usejs_average = ((float) SW. elapsedmilliseconds)/1000000;
Sw. Reset ();
Sw. Start ();
i = 1000000;
while (i--> 0)
{
int II = 1;
int JJ = 2;
int sum = 0;
sum = II + JJ;
}
Sw. Stop ();
Long Nousejs_total = SW. Elapsedmilliseconds;
Float Nousejs_ Average = ((float) SW. elapsedmilliseconds)/1000000;
String msg = "";
msg = string. Format ("Usejs_total: {0}\r\n", usejs_total);
msg = string. Format ("Usejs_average: {0}\r\n\r\n\r\n", usejs_average);
msg = string. Format ("Nousejs_total: {0}\r\n", nousejs_total);
msg = string. Format ("Nousejs_average: {0}\r\n\r\n\r\n", nousejs_average);
MessageBox.Show (msg);
}
}
Results (above is calculated by the JS engine time, the following is pure C # calculation time, tragedy AH): This noesis.javascript only apply to the performance requirements of the occasion