Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于名称动态调用方法的有效方法,无需反射_C#_Reflection_Delegates - Fatal编程技术网

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

C# 基于名称动态调用方法的有效方法,无需反射,c#,reflection,delegates,C#,Reflection,Delegates,让我试着简化我的问题: 我有四个类:管理员、用户、玩家、角色 数据库返回我需要执行的方法的名称。例如,如果返回了Admins\u GetName,则需要对Admins类执行GetName()方法。 如果返回Players\u GetRank,则需要在Players类上调用GetRank()方法 我不想写一个包含所有业务逻辑的庞大的IF或SWITCH语句。如果不使用反射,什么是最有效的解决方案?如果可能的话,我想避免反射带来的性能冲击 请记住,所有方法都可能有不同的参数,但会返回字符串 下面是我现

让我试着简化我的问题:

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

数据库返回我需要执行的方法的名称。例如,如果返回了
Admins\u GetName
,则需要对
Admins
类执行
GetName()
方法。 如果返回
Players\u 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()
你们能给我建议一个最好的方法来完成这个任务吗?或者帮我找到正确的语法来实现它


谢谢。

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

否则,
开关
语句方法是合理的,但有维护开销。如果这有问题,我会尝试在运行时构建委托缓存,例如:

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 ""; }
}
使用系统;
使用System.Collections.Generic;
公共课程
{
公共字符串条{get;set;}
静态void Main()
{
var foo=new foo();
执行(foo,“B”);
执行(foo,“D”);
}
}
静态类FooUtils
{
公共静态void Execute(Foo-Foo,string-methodName)
{
methodCache[methodName](foo);
}
静态只读字典方法缓存;
静态FooUtils()
{
methodCache=newdictionary();
foreach(typeof(Foo.GetMethods()中的var方法)
{
如果(!method.IsStatic&&method.ReturnType==typeof(void)
&&方法。GetParameters().Length==0)
{
添加(method.Name,(操作)
CreateDelegate(typeof(Action),method));
}
}
}
}
公开课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”);}
已忽略公共字符串(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));
                }
            }
        }
    }
}
静态类FooUtils
{
publicstaticvoidexecute(T目标,stringmethodname)
{
Execute(target,methodName);
}
静态类MethodCache
{
publicstaticvoidexecute(T目标,stringmethodname)
{
methodCache[methodName](目标);
}
静态只读字典方法缓存;
静态MethodCache()
{
methodCache=newdictionary();
foreach(typeof(T).GetMethods()中的var方法)
{
如果(!method.IsStatic&&method.ReturnType==typeof(void)
&&方法。GetParameters().Length==0)
{
添加(method.Name,(操作)
CreateDelegate(typeof(Action),method));
}
}
}
}
}

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

否则,
开关
语句方法是合理的,但有维护开销。如果这有问题,我会尝试在运行时构建委托缓存,例如:

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 ""; }
}
使用系统;
使用System.Collections.Generic;
公共课程
{
公共字符串条{get;set;}
静态void Main()
{
var foo=new foo();
执行(foo,“B”);
执行(foo,“D”);
}
}
静态类FooUtils
{
公共静态void Execute(Foo-Foo,string-methodName)
{
methodCache[methodName](foo);
}
静态只读字典方法缓存;
静态FooUtils()
{
methodCache=newdictionary();
foreach(typeof(Foo.GetMethods()中的var方法)
{
如果(!method.IsStatic&&method.ReturnType==typeof(void)
&&方法。GetParameters().Length==0)
{
添加(method.Name,(操作)
CreateDelegate(typeof(Action),method));
}
}
}
}
公开课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”);}
已忽略公共字符串(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));
                }
            }
        }
    }
}
静态类FooUtils
{
publicstaticvoidexecute(T目标,stringmethodname)
{
Execute(target,methodName);
}
静态类MethodCache
{
publicstaticvoidexecute(T目标,stringmethodname)
{
methodCache[methodName](目标);
}
静态只读字典方法缓存;
静态MethodCache()
{
methodCache=newdictionary();
foreach(typeof(T).GetMethods()中的var方法)