繁体   English   中英

基于名称的动态调用方法的有效方法,无需反射

[英]Efficient way to call a method dynamically based on its name without reflection

让我尝试简化我的问题:

我有四个类别:管理员,用户,播放器,角色

数据库返回我需要执行的方法的名称。 例如,如果返回了Admins_GetName则需要在Admins类上执行GetName()方法。 如果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
}

返回类/方法参考;

2)将声明从上方传递到:

var myValue = Retrieved method.invoke()

你们能以最好的方式向我提出建议,还是以正确的语法帮助我实现该想法。

谢谢。

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

否则, 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 ""; }
}

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

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

暂无
暂无

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

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