简体   繁体   English

如何获取具有可变数量的参数的函数的ParameterInfo?

[英]How to get the ParameterInfo of a function with variable number of params?

How to get the ParameterInfo of a function with variable number of params? 如何获取具有可变数量的参数的函数的ParameterInfo? The problem is when I call the method 问题是当我调用该方法时

MyFunction(object o1, out object o2);

I can get the parameterInfo of sendData but not the o1 and o2 object. 我可以获取sendData的parameterInfo但不能获取o1和o2对象。

protected object[] MyFunction(params object[] sendData)
{
     StackTrace callStack = new StackTrace(0, false);
     StackFrame callingMethodFrame = callStack.GetFrame(0);
     MethodBase callingMethod = callingMethodFrame.GetMethod();
     ParameterInfo[] parametersInfo = callingMethod.GetParameters();

     List<object> inParams = new List<object>();
     List<object> outParams = new List<object>();

     for (int i = 0; i < sendData.Length; i++)
     {
         object value = sendData[i];
         ParameterInfo info = parametersInfo[parametersInfo.Length - sendData.Length + i];

         if (info.IsOut)
         {
              outParams.Add(value);
         }
         else
         {
              inParams.Add(value);
         }
     }
     ..........
}

Thanks in advance for helping me. 在此先感谢您的帮助。

Arnaud 阿尔诺

'params' is just C# syntactic sugar. 'params'只是C#的语法糖。 In fact, at metadata .NET level, there is only one parameter named "sendData" with a specific "ParamArray" attribute set. 实际上,在元数据.NET级别,只有一个名为“sendData”的参数具有特定的“ParamArray”属性集。

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

相关问题 如何从反射中的 ParameterInfo[] 获得真正的价值 - How to get real value from the ParameterInfo[] in Reflection .NET Reflection - 如何从ParameterInfo中获取“真实”类型 - .NET Reflection - How to get “real” type from out ParameterInfo 获取ParameterInfo参数的参考 - Get reference of ParameterInfo parameter 如何确定ParameterInfo是否是泛型类型? - How to determine if ParameterInfo is of generic type? 如何确定ParameterInfo是否为返回参数 - How to determine if a ParameterInfo is a return parameter 如何判断ParameterInfo类型是否为集合? - How to tell if ParameterInfo type is a collection? 我们如何使用 `params object[] args` 将可变数量的参数传送到另一种方法? - How do we barrel a variable number of params using `params object[] args` to another method? 确定Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo是否为变量? - Determine if Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo is a Variable? C#为可变数量的参数构建参数列表的简单方法 - C# simple way to build params list for variable number of params 如何将任意lambda作为参数传递给函数(以获取命名层次结构)? - How to pass arbitrary lambdas as params to function (to get naming hierarchy)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM