C# 创建函数<;T、 T>;使用MethodInfo

C# 创建函数<;T、 T>;使用MethodInfo,c#,generics,reflection,delegates,action,C#,Generics,Reflection,Delegates,Action,我正在尝试使用反射来自动化方法存储库 例如,我有一个方法 public string CanCallThis(int moduleFunctionId) { return "Hello"; } 我在我的命令管理器中这样注册 commandManager.RegisterCommand(moduleName,"CanCallThis", new ModuleCommand<int, string>(CanCallThis)); List<MethodInfo>

我正在尝试使用反射来自动化方法存储库

例如,我有一个方法

public string CanCallThis(int moduleFunctionId)
{
    return "Hello";
}
我在我的命令管理器中这样注册

commandManager.RegisterCommand(moduleName,"CanCallThis", new ModuleCommand<int, string>(CanCallThis));
List<MethodInfo> methodInfos = IdentifyMethods();

foreach (var methodInfo in methodInfos)
{
    ParameterInfo[] methodParams = methodInfo.GetParameters();

    if (methodParams.Length = 1)
    {
        Type returnType = methodInfo.ReturnType;
        string methodName = methodInfo.Name;
        Type inputParam = methodParams[0].ParameterType;

        commandManager.RegisterCommand(moduleName, methodInfo.Name, new ModuleCommand<inputParam, returnType>(unknown));
    }
}
commandManager.RegisterCommand(moduleName,“CanCallThis”,newmodulecommand(CanCallThis));
这很好,但是注册每个命令是一个手动过程

我正在尝试使用反射,这样我就可以探测类中的命令,然后使用反射发现的信息调用RegistrCommand方法-这将使生活变得更加简单,因为我不必记住添加每个RegistrCommand条目

我正在创建注册该方法的方法,目前我的代码如下所示

commandManager.RegisterCommand(moduleName,"CanCallThis", new ModuleCommand<int, string>(CanCallThis));
List<MethodInfo> methodInfos = IdentifyMethods();

foreach (var methodInfo in methodInfos)
{
    ParameterInfo[] methodParams = methodInfo.GetParameters();

    if (methodParams.Length = 1)
    {
        Type returnType = methodInfo.ReturnType;
        string methodName = methodInfo.Name;
        Type inputParam = methodParams[0].ParameterType;

        commandManager.RegisterCommand(moduleName, methodInfo.Name, new ModuleCommand<inputParam, returnType>(unknown));
    }
}
List methodInfos=IdentifyMethods();
foreach(methodInfo中的var methodInfo)
{
ParameterInfo[]methodParams=methodInfo.GetParameters();
如果(methodParams.Length=1)
{
类型returnType=methodInfo.returnType;
字符串methodName=methodInfo.Name;
类型inputParam=methodParams[0]。参数类型;
commandManager.RegisterCommand(moduleName,methodInfo.Name,newmoduleCommand(未知));
}
}
在上面的示例中,inputParam、returnType和unknown导致遵从性错误。我的目标是创建ModuleCommand的实例。 我确信这与创建一个委托有关,但我不确定如何进行


有人能帮我创建模块命令吗?

找到了解决方案,这是给大家的

List<MethodInfo> methodInfos = IdentifyMethods();

foreach (var methodInfo in methodInfos)
{
    ParameterInfo[] methodParams = methodInfo.GetParameters();

    if (methodParams.Length == 1)
    {
        Type returnType = methodInfo.ReturnType;
        string methodName = methodInfo.Name;
        Type inputParam = methodParams[0].ParameterType;

        Type genericFuncType = typeof(Func<,>).MakeGenericType(inputParam, returnType);
        Delegate methodDelegate = Delegate.CreateDelegate(genericFuncType, this, methodInfo);

        Type genericModuleCommandType = typeof(ModuleCommand<,>).MakeGenericType(inputParam, returnType);

        IModuleCommand o = (IModuleCommand)Activator.CreateInstance(genericModuleCommandType, methodDelegate);

        commandManager.RegisterCommand(moduleName, methodName, o);
    }
}
List methodInfos=IdentifyMethods();
foreach(methodInfo中的var methodInfo)
{
ParameterInfo[]methodParams=methodInfo.GetParameters();
如果(methodParams.Length==1)
{
类型returnType=methodInfo.returnType;
字符串methodName=methodInfo.Name;
类型inputParam=methodParams[0]。参数类型;
Type genericFuncType=typeof(Func).MakeGenericType(inputParam,returnType);
Delegate methodDelegate=Delegate.CreateDelegate(genericFuncType,this,methodInfo);
类型genericModuleCommandType=typeof(ModuleCommand).MakeGenericType(inputParam,returnType);
IModuleCommand o=(IModuleCommand)Activator.CreateInstance(genericModuleCommandType,methodDelegate);
注册表命令(moduleName,methodName,o);
}
}

上面代码中的IModuleCommand是我为ModuleCommand实现的接口创建的。这是在Registercommand方法上实现的。

我猜您的一个编译器错误可能是重复的:
methodParams.Length=1
如果您注册并使用反射调用泛型委托,我想知道为什么您会有泛型委托?在这种情况下,不需要编译时类型。这让事情变得更复杂。直接注册一个
MethodInfo
怎么样?Type genericType=typeof(ModuleCommand);IModuleCommand o=(IModuleCommand)Activator.CreateInstance(genericType,null);更进一步,但现在调用Activator.CreateInstance时遇到问题,因为这需要将参数传递给构造函数,参数就是方法本身。