原文地址:http://www.cnblogs.com/vipsoft/archive/2012/10/19/2731126.html
根据传进来不同的值,调用不同的方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
protected void btn_SwitchClick(object sender, EventArgs e)
{
string result = ""; switch (ddlMethod.SelectedValue) { case "A": result = SwitchTest.GetA(); break; case "B": result = SwitchTest.GetB(); break; case "C": result = SwitchTest.GetC(); break; default: result = ddlMethod.SelectedValue + "方法找不到"; break; } ltrResult.Text = result; }
下面利用反射机制实现,首选需要一个自定义属性类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class ActionMethodAttribute:Attribute
{
public string ActionTypeName; public ActionMethodAttribute(string typeName) { this.ActionTypeName = typeName; } }
然后定义一个基类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public abstract class GenericBLL
{
public Hashtable GetMethodAttribute<T>(T t) { Hashtable ht = new Hashtable(); Hashtable obj = CacheHandler<Hashtable>.GetCache(t.ToString()); if (obj == null) { Type type = t.GetType(); foreach (MethodInfo mi in type.GetMethods()) { ActionMethodAttribute[] mis = (ActionMethodAttribute[])mi.GetCustomAttributes(typeof(ActionMethodAttribute), false); foreach (ActionMethodAttribute actionMethodAttribute in mis) { string actionName = actionMethodAttribute.ActionTypeName; ht.Add(actionName, mi); } } CacheHandler<Hashtable>.SetCache(t.ToString(), ht); } else { ht = (Hashtable)obj; } return ht; } /// <summary> /// return message; /// </summary> /// <param name="actionName"></param> /// <returns></returns> public string DoAction(string actionName) { string message; Hashtable ht = GetMethodAttribute(this); if (ht.Contains(actionName)) { message = ((MethodInfo)ht[actionName]).Invoke(this, new object[] { }).ToString(); } else { message = string.Format("{0} Not Defined.!", actionName); //throw new Exception(errmsg); } return message; } }
实现类继承,
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class ReflectTest:GenericBLL
{
[ActionMethod("A")] public string GetA() { return "调用的A"; } [ActionMethod("B")] public string GetB() { return "调用的B"; } [ActionMethod("C")] public string GetC() { return "调用的C"; } }
具体的调用
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
protected void btn_ReflectClick(object sender, EventArgs e)
{
string result = ReflectTest.DoAction(ddlMethod.SelectedValue); ltrResult.Text = result; }
ASPX中的代码如下
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
选D会提示没有D方法
<asp:DropDownList ID="ddlMethod" runat="server"> <asp:ListItem Text="A" Value="A"> </asp:ListItem> <asp:ListItem Text="B" Value="B"> </asp:ListItem> <asp:ListItem Text="C" Value="C"> </asp:ListItem> <asp:ListItem Text="D" Value="D"> </asp:ListItem> </asp:DropDownList> <br /> <asp:Button ID="btnInvoke" Text="Switch" OnClick="btn_SwitchClick" runat="server" /> <asp:Button ID="btnInvokeR" Text="Reflect" OnClick="btn_ReflectClick" runat="server" /> <br> <asp:Literal ID="ltrResult" runat="server" />
原代码下载: ActionMethod.rar