簡體   English   中英

如何獲取具有可變數量的參數的函數的ParameterInfo?

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

如何獲取具有可變數量的參數的函數的ParameterInfo? 問題是當我調用該方法時

MyFunction(object o1, out object o2);

我可以獲取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);
         }
     }
     ..........
}

在此先感謝您的幫助。

阿爾諾

'params'只是C#的語法糖。 實際上,在元數據.NET級別,只有一個名為“sendData”的參數具有特定的“ParamArray”屬性集。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM