根据名称动态调用方法而无需反射的有效方法

Ins*_*dBy 3 c# reflection delegates

让我尝试简化我的问题:

我有四个类别:管理员、用户、玩家、角色

数据库返回我需要执行的方法的名称。例如,如果Admins_GetName返回,则GetName()需要在Admins类上执行方法。如果Players_GetRank返回,则GetRank()需要在Players类上调用方法。

我不想写一个巨大的 IF 或 SWITCH 语句,其中包含我的所有业务逻辑。不使用反射最有效的解决方案是什么?如果可能的话,我想避免反射带来的性能影响。

请记住,所有方法可能有不同的参数,但都会返回字符串。

这是我现在想做的:1)有一个带有 switch 语句的方法,它将分解数据库值并找到我需要执行的类和方法。就像是:

switch(DbValue)
{
 case DbValue == "Admins_GetName":
  Declare a delegate to Admins.GetName();
  return;
 case: DbValue = "Players_GetRank"
  Declare a delegate to Players.GetRank();
  return;
  . 
  .
  .
  etc
}
Run Code Online (Sandbox Code Playgroud)

返回类/方法引用;

2) 将上述声明传递至:

var myValue = Retrieved method.invoke()
Run Code Online (Sandbox Code Playgroud)

你们能否建议我完成此任务的最佳方法,或者帮助我使用正确的语法来实现我的想法。

谢谢。

Mar*_*ell 5

需要更多背景信息;例如,所有相关方法是否都具有相同的签名?在一般情况下,反射是最合适的工具,只要您没有在紧密循环中调用它,就可以了。

否则,switch声明方式是合理的,但具有维护开销。如果这是有问题的,我会想在运行时构建委托缓存,例如:

using System;
using System.Collections.Generic;
public class Program
{
    public string Bar { get; set; }

    static void Main()
    {
        var foo = new Foo();
        FooUtils.Execute(foo, "B");
        FooUtils.Execute(foo, "D");
    }
}
static class FooUtils
{
    public static void Execute(Foo foo, string methodName)
    {
        methodCache[methodName](foo);
    }
    static readonly Dictionary<string, Action<Foo>> methodCache;
    static FooUtils()
    {
        methodCache = new Dictionary<string, Action<Foo>>();
        foreach (var method in typeof(Foo).GetMethods())
        {
            if (!method.IsStatic && method.ReturnType == typeof(void)
                && method.GetParameters().Length == 0)
            {
                methodCache.Add(method.Name, (Action<Foo>)
                    Delegate.CreateDelegate(typeof(Action<Foo>), method));
            }
        }
    }
}

public class Foo
{
    public void A() { Console.WriteLine("A"); }
    public void B() { Console.WriteLine("B"); }
    public void C() { Console.WriteLine("C"); }
    public void D() { Console.WriteLine("D"); }
    public string Ignored(int a) { return ""; }
}
Run Code Online (Sandbox Code Playgroud)

通过使用泛型,该方法可以扩展到多种目标类型:

static class FooUtils
{
    public static void Execute<T>(T target, string methodName)
    {
        MethodCache<T>.Execute(target, methodName);
    }
    static class MethodCache<T>
    {
        public static void Execute(T target, string methodName)
        {
            methodCache[methodName](target);
        }
        static readonly Dictionary<string, Action<T>> methodCache;
        static MethodCache()
        {
            methodCache = new Dictionary<string, Action<T>>();
            foreach (var method in typeof(T).GetMethods())
            {
                if (!method.IsStatic && method.ReturnType == typeof(void)
                    && method.GetParameters().Length == 0)
                {
                    methodCache.Add(method.Name, (Action<T>)
                        Delegate.CreateDelegate(typeof(Action<T>), method));
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)