首页 > 其他分享 >利用反射代替switch

利用反射代替switch

时间:2022-12-24 10:04:26浏览次数:33  
标签:反射 return string ht switch 代替 result public Hashtable

根据传进来不同的值,调用不同的方法

View Code

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;
}

下面利用反射机制实现,首选需要一个自定义属性类

View Code

public class ActionMethodAttribute:Attribute
{
public string ActionTypeName;

public ActionMethodAttribute(string typeName)
{
this.ActionTypeName = typeName;
}
}

然后定义一个基类

View Code

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;
}
}

实现类继承,

View Code

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";
}
}

具体的调用

View Code

protected void btn_ReflectClick(object sender, EventArgs e)
{
string result = ReflectTest.DoAction(ddlMethod.SelectedValue);
ltrResult.Text = result;
}

ASPX中的代码如下

View Code

选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​​



标签:反射,return,string,ht,switch,代替,result,public,Hashtable
From: https://blog.51cto.com/u_15116285/5967063

相关文章

  • FreeSWITCH学习笔记:EventSocket
    本文更新于2022-12-20,使用FreeSWITCH1.10.7。目录apiauthbgapiconnectdivert_eventseventexitfilterfilterdeletelingerlogmyeventsnixeventnoeventnolingernologsendev......
  • 反射
    反射:框架设计的灵魂  *框架:半成品软件。可以在框架的基础上进行软件的开发,简化编码。  *反射:将类的各个组成部分封装为其他对象,这就是反射机制。   *好处:  ......
  • 重学c#系列—— 反射深入一点点[三十三]
    前言在上一章中介绍了什么是反射:https://www.cnblogs.com/aoximin/p/16440966.html正文上一节讲述反射的基本原理和为什么要用反射,还用反射的优缺点这些。其二者的......
  • freeswitch的gateway配置方案
      概述freeswitch是一款简单好用的VOIP开源软交换平台。在voip的网络模型中,网关是我们经常会遇到的概念。在freeswitch中,如何配置gateway,如何使用好gateway的模型......
  • java switch语法
    javaswitch语法switch(type){case"aaa":break;case"bbb":break;case"ccc":......
  • 使用ScheduledExecutorService代替下Timer
    使用ScheduledExecutorService代替下Timer1.scheduledExecutorService类方法scheduleAtFixedRate,ScheduleWithFixedDelay区别比较1.1.ScheduleAtFixedRate两次任务之间......
  • switch多选择结构、循环结构
    switch多选择结构多选择结构还有一个实现方式就是switchcase语句switchcase语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。switch语句中的变......
  • java反射--PropertyDescriptor类:(属性描述器)、Introspector类
    博客后台-博客园(cnblogs.com)packagecom.peidasoft.instrospector;importjava.beans.BeanInfo;importjava.beans.Introspector;importjava.beans.Prop......
  • 重学c#系列—— 反射的基本理解[三十三]
    前言在上一章中介绍了什么是反射:https://www.cnblogs.com/aoximin/p/16440966.html正文上一节讲述反射的基本原理和为什么要用反射,还用反射的优缺点这些。其二者的......
  • 反射 指针 字符串
     func largestMerge(s, t string) string {    n := len(s)    sa := *(*[]int32)(unsafe.Pointer(reflect.ValueOf(suffixarray.New([]byte(s + ......