通過反射計算方法執行時間 包括out參數

/// <summary>
        /// 獲得方法的執行時間
        /// </summary>
        /// <param name="ClassType">類</param>
        /// <param name="ClassParameters">構造函數用的參數</param>
        /// <param name="MethodName">方法名稱</param>
        /// <param name="MethodParameters">方法參數</param>
        /// <returns>毫秒</returns>
        public static long GetMethodRunTime(Type ClassType, object[] ClassParameters, string MethodName, object[] MethodParameters,Type[] MethodParametersType)
        {
            object obj = Activator.CreateInstance(ClassType, ClassParameters);
            MethodInfo method = ClassType.GetMethod(MethodName, MethodParametersType);
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            method.Invoke(obj, MethodParameters);
            stopWatch.Stop();
            return stopWatch.ElapsedTicks;///Stopwatch.Frequency/1000;
            
        }

int x = -1;
            long time =  Helper.GetMethodRunTime(typeof(T_RoleService),null,"QueryByPage",new object[]{1,x},new Type[]{ typeof(Int32),typeof(int).MakeByRefType() });
            MessageBox.Show(time.ToString());

注意typeof(int).MakeByRefType(),這裏關聯out
發佈了116 篇原創文章 · 獲贊 7 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章