【问题标题】:Efficient way to call a method dynamically based on its name without reflection根据名称动态调用方法而无需反射的有效方法
【发布时间】:2012-12-19 06:26:05
【问题描述】:

让我试着简化我的问题:

我有四个类:Admins、Users、Players、Roles

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

我不想编写包含我所有业务逻辑的巨大 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 语句。
  • 想想他们总是会返回字符串

标签: c# reflection delegates


【解决方案1】:

需要更多的上下文;例如,是否所有有问题的方法都具有相同的签名?在一般的情况下,反射是最合适的工具,只要你不是在一个紧密的循环中调用它,就可以了。

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

【讨论】:

  • 方法总是返回字符串
  • 你可以实现 Func: static Dictionary> MethodCache = new Dictionary >(); static void Execute(string methodName, string a) { var result = MethodCache[methodName](a); { static void Initialize(Foo foo) { foreach(var method in typeof(Foo).GetMethods()) { if(!method.IsStatic && method.ReturnType == typeof(string) // 此处检查其他参数... MethodCache .Add(method.Name, (Func), Delegate.CreateDelegate(typeof(Func), foo, method)); } }
猜你喜欢
  • 2011-07-18
  • 2011-07-18
  • 2013-07-23
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-24
相关资源
最近更新 更多