简体   繁体   English

c#基本功能串联,避免使用switch

[英]c# basic function concatenation to avoid using switch

I am looking for a smarter way to call functions without using switch. 我正在寻找一种更智能的方法来调用函数而不使用switch。 My problem is as below: I have many function calls that read something like this.map.functionApple(); 我的问题如下:我有许多函数调用,读取类似this.map.functionApple(); this.map.functionPeach(); this.map.functionPeach(); this.map.functionOrange(); this.map.functionOrange();

I am currently calling a parent function fruit("Orange") which basically is a huge function with switch which selects the function this.map.functionOrange() Is there a way where I can simply do: 我正在调用一个父函数fruit("Orange") ,它基本上是一个巨大的函数,用switch选择函数this.map.functionOrange()有没有办法,我可以简单地做:

this.map.function+name+();

There are a few basic ways to handle branching. 处理分支有几种基本方法。 You have the if ... else construct, which allows you to create two or more branches (else if). 你有if ... else构造,它允许你创建两个或更多分支(否则如果)。 Then there is a switch, which simplifies the if ... else logic. 然后有一个开关,简化了if ... else逻辑。

Another way is to look up the answer. 另一种方法是查找答案。 This can be in a config file, a database or hashed in some way. 这可以在配置文件,数据库中或以某种方式进行散列。 Jon Skeet recommends a Dictionary with an action, which is one way of handling this via a hash construct. Jon Skeet建议使用一个带有动作的Dictionary,这是通过哈希构造处理它的一种方法。

Corey's suggestion of Command or Strategy do not solve the branching logic, per se, but give you a structured way of handling it. Corey对命令或策略的建议本身并不能解决分支逻辑,而是为您提供一种结构化的处理方式。 Using the Strategy pattern, for example, you can easily add new strategies to handle other fruit. 例如,使用策略模式,您可以轻松添加新策略来处理其他水果。 Command also does not completely solve the branching "issue", but can be set up to solve the issue of having multiple types of fruit with different actions. 命令也没有完全解决分支“问题”,但可以设置为解决具有不同动作的多种类型水果的问题。

If i understood correctly you are looking for something like below: 如果我理解正确您正在寻找以下内容:

using System.Reflection;
string name = "Orange";//Put your function name here....

//In your calling function:
MethodInfo yourMethod = this.map.GetType().GetMethod("function" + name);
object magicValue = magicMethod.Invoke(this,null);

Building on Jon's comment, using a Dictionary<string, action> is a simple and elegant approach. 基于Jon的评论,使用Dictionary<string, action>是一种简单而优雅的方法。 It would look something like this: 它看起来像这样:

class StackOverflowExample
{
    private MapThing _map;
    private IDictionary<string, Action> _mapActions;

    public StackOverflowExample()
    {
        _map = new MapThing();

        _mapActions = new Dictionary<string, Action>
        {
            { "orange", _map.functionOrange },
            { "peach", _map.functionPeach },
            ...
        }
    }

    public void Fruit(string name)
    {
        // Now, instead of the giant switch statement...
        if (_mapActions.ContainsKey(name))
            _mapActions[name].Invoke();

        // Throw an exception if name not in dictionary
    }
}

Simple usage: 用法简单:

var example = new StackOverflowExample();
example.Fruit("orange");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM