Calling a function using reflection that has a quot;paramsquot; parameter (MethodBase)(使用具有“参数的反射调用函数.参数(方法库))
问题描述
我有两个函数的 MethodBase:
I have MethodBases for two functions:
public static int Add(params int[] parameters) { /* ... */ }
public static int Add(int a, int b) { /* ... */ }
我有一个通过我创建的类调用 MethodBases 的函数:
I have a function that calls the MethodBases via a class I made:
MethodBase Method;
object Target;
public object call(params object[] input)
{
    return Method.Invoke(Target, input);
}
现在如果我 AddTwoMethod.call(5, 4); 它工作正常.
Now if I AddTwoMethod.call(5, 4); it works fine.
如果我使用 AddMethod.call(5, 4); 它会返回:
If I however use AddMethod.call(5, 4); it returns:
未处理的异常:System.Reflection.TargetParameterCountException:参数与签名不匹配
Unhandled Exception: System.Reflection.TargetParameterCountException: parameters do not match signature
有什么方法可以使两个调用都能正常工作,而无需手动将参数放入 params int[] 的数组中?
Is there any way to make it so that both calls work fine without need for manually putting the arguments in an array for the params int[]?
推荐答案
您可以修改您的 call 方法以检测 params 参数并将输入的其余部分转换为新数组.这样一来,您的方法的行为就与 C# 应用于方法调用的逻辑几乎相同.
You could modify your call method to detect the params parameter and convert the rest of the input to a new array. That way your method could act pretty much the same as the logic C# applies to the method calling.
我为您快速构建的东西(请注意,我以非常有限的方式测试了此方法,因此可能仍然存在错误):
Something i quicly constructed for you (be aware that i tested this method in a pretty limited way, so there might be errors still):
public object call(params object[] input)
{
    ParameterInfo[] parameters = Method.GetParameters();
    bool hasParams = false;
    if (parameters.Length > 0)
        hasParams = parameters[parameters.Length - 1].GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0;
    if (hasParams)
    {
        int lastParamPosition = parameters.Length - 1;
        object[] realParams = new object[parameters.Length];
        for (int i = 0; i < lastParamPosition; i++)
            realParams[i] = input[i];
        Type paramsType = parameters[lastParamPosition].ParameterType.GetElementType();
        Array extra = Array.CreateInstance(paramsType, input.Length - lastParamPosition);
        for (int i = 0; i < extra.Length; i++)
            extra.SetValue(input[i + lastParamPosition], i);
        realParams[lastParamPosition] = extra;
        input = realParams;
    }
    return Method.Invoke(Target, input);
}
请注意,我以非常有限的方式测试了此方法,因此可能仍然存在错误.
Be aware that i tested this method in a pretty limited way, so there might be errors still.
这篇关于使用具有“参数"的反射调用函数.参数(方法库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用具有“参数"的反射调用函数.参数(方法库)
				
        
 
            
        - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 
						
						
						
						
						
				
				
				
				