C # can reduce or not use switch what method _ Practical Tips

Source: Internet
Author: User
Tags reflection
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");

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.