The Insus.net solution is to use factory methods to process, create a factory interface, and then each method is designed as a factory class and implements the factory interface.
Factory Interface:
Copy Code code as follows:
Igetfactory
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
Summary description for Igetfactory
</summary>
Namespace Insus.net
{
public interface Igetfactory
{
String GetResult ();
}
}
Get factory class:
Copy Code code as follows:
GetFactory
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
Summary description for GetFactory
</summary>
Namespace Insus.net
{
public class Getfactory:igetfactory
{
Public GetFactory ()
{
//
Todo:add constructor Logic here
//
}
public string GetResult ()
{
return ' get ';
}
}
}
Gettest class:
Copy Code code as follows:
Gettestfactory
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
Summary description for Gettestfactory
</summary>
Namespace Insus.net
{
public class Gettestfactory:igetfactory
{
Public Gettestfactory ()
{
//
Todo:add constructor Logic here
//
}
public string GetResult ()
{
return "Gettest";
}
}
}
and Getset class:
Copy Code code as follows:
Getsetfactory
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
Summary description for Getsetfactory
</summary>
Namespace Insus.net
{
public class Getsetfactory:igetfactory
{
Public Getsetfactory ()
{
//
Todo:add constructor Logic here
//
}
public string GetResult ()
{
return "Getset";
}
}
}
so your code eventually becomes:
Copy Code code as follows:
View Code
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Insus.net;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
}
public string Exec (string mothedname)
{
string ret = "";
Switch (mothedname)
//{
Case "GET":
ret = Get ();
Break
Case "Get1":
ret = Gettest ();
Break
// //.....
Case "Testget":
ret = Getrset ();
Break
//}
Igetfactory get = new Gettestfactory (); Here is the implementation of the factory class
ret = Get. GetResult ();
return ret;
}
public string Get ()
//{
return ' get ';
//}
public string Gettest ()
//{
return "Gettest";
//}
public string Getrset ()
//{
return "Getset";
//}
}
15:50 modified to add the following:
The above final code, no incoming parameters mothedname, how to do, we can consider reflection, if the reverse shooting, the incoming parameters need to standardize the side can:
' Get ' >> ' get ';
"Get1" >> "Gettest"
"Testget" >> "Getset"
After such a change, you can use the reflection syntax, you can
Copy Code code as follows:
Igetfactory get = new Gettestfactory (); Here is the implementation of the factory class
Instead (the following is the application of asp.net):
Copy Code code as follows:
Igetfactory get = (igetfactory) assembly.load ("App_Code"). CreateInstance ("insus.net." + mothedname + "Factory");
If you are not asp.net, you can change "App_Code" to "assembly name":
Copy Code code as follows:
Igetfactory get = (igetfactory) assembly.load ("assembly name"). CreateInstance ("insus.net." + mothedname + "Factory");