Example #1
0
        /// <summary>
        /// Uses reflection to dynamically invoke a method,
        /// throwing an exception if it is not implemented
        /// on the target object.
        /// </summary>
        /// <param name="obj">
        /// Object containing method.
        /// </param>
        /// <param name="methodHandle">
        /// MethodHandle for the method.
        /// </param>
        /// <param name="parameters">
        /// Parameters to pass to method.
        /// </param>
        private static object CallMethod(object obj, DynamicMethodHandle methodHandle, params object[] parameters)
        {
            var method = methodHandle.DynamicMethod;

            object[] inParams = parameters == null ? new object[] { null } : parameters;
            //如果最后一个参数是一个 param 数组,则尝试把参数动态添加到这个数组中。
            if (methodHandle.HasFinalArrayParam)
            {
                bool rebuildParameters = true;

                //四种情况:实参比形参少一个;实参多于形参;
                //实参与形参相等,且最后一个参数是数组的元素;
                //实参与形参相等,且最后一个参数就是数组;
                //其中,只有最后一种情况下,不需要重新构造一个新的参数列表。
                var pCount = methodHandle.MethodParamsLength;
                if (parameters.Length == pCount)
                {
                    var last = parameters[pCount - 1];
                    if (last is Array)
                    {
                        rebuildParameters = false;
                    }
                }

                if (rebuildParameters)
                {
                    //构造一个全新的参数列表集合,并把新集合中除最后一个位置外的元素全部从 imParams 拷贝。
                    object[] newParameterList = new object[pCount];
                    Array.Copy(inParams, newParameterList, pCount - 1);

                    //计算出剩余的所有参数的个数,并构造一个数组,把剩余的所有参数都拷贝进这个数组中。
                    var paramArrayLength = inParams.Length - (pCount - 1);
                    var paramArray       = Array.CreateInstance(methodHandle.FinalArrayElementType, paramArrayLength);
                    Array.Copy(inParams, pCount - 1, paramArray, 0, paramArrayLength);
                    newParameterList[newParameterList.Length - 1] = paramArray;

                    //修改为使用新的集合。
                    inParams = newParameterList;
                }
            }

            var result = methodHandle.DynamicMethod(obj, inParams);

            return(result);
        }
Example #2
0
        private static DynamicMethodHandle GetCachedMethod(MethodInfo info)
        {
            var key = new MethodCacheKey(info);
            DynamicMethodHandle mh = null;

            if (!_methodCache.TryGetValue(key, out mh))
            {
                lock (_methodCache)
                {
                    if (!_methodCache.TryGetValue(key, out mh))
                    {
                        mh = new DynamicMethodHandle(info);
                        _methodCache.Add(key, mh);
                    }
                }
            }
            return(mh);
        }
Example #3
0
        private static DynamicMethodHandle GetCachedMethod(Type objType, string method, params Type[] parameters)
        {
            var key = new MethodCacheKey(objType, method, parameters);
            DynamicMethodHandle mh = null;

            if (!_methodCache.TryGetValue(key, out mh))
            {
                MethodInfo info = GetMethod(objType, method, parameters);
                if (info != null)
                {
                    lock (_methodCache)
                    {
                        if (!_methodCache.TryGetValue(key, out mh))
                        {
                            mh = new DynamicMethodHandle(info);
                            _methodCache.Add(key, mh);
                        }
                    }
                }
            }
            return(mh);
        }
Example #4
0
 private static DynamicMethodHandle GetCachedMethod(Type objType, string method, params Type[] parameters)
 {
     var key = new MethodCacheKey(objType, method, parameters);
     DynamicMethodHandle mh = null;
     if (!_methodCache.TryGetValue(key, out mh))
     {
         MethodInfo info = GetMethod(objType, method, parameters);
         if (info != null)
         {
             lock (_methodCache)
             {
                 if (!_methodCache.TryGetValue(key, out mh))
                 {
                     mh = new DynamicMethodHandle(info);
                     _methodCache.Add(key, mh);
                 }
             }
         }
     }
     return mh;
 }
Example #5
0
 private static DynamicMethodHandle GetCachedMethod(MethodInfo info)
 {
     var key = new MethodCacheKey(info);
     DynamicMethodHandle mh = null;
     if (!_methodCache.TryGetValue(key, out mh))
     {
         lock (_methodCache)
         {
             if (!_methodCache.TryGetValue(key, out mh))
             {
                 mh = new DynamicMethodHandle(info);
                 _methodCache.Add(key, mh);
             }
         }
     }
     return mh;
 }
Example #6
0
        /// <summary>
        /// Uses reflection to dynamically invoke a method,
        /// throwing an exception if it is not implemented
        /// on the target object.
        /// </summary>
        /// <param name="obj">
        /// Object containing method.
        /// </param>
        /// <param name="methodHandle">
        /// MethodHandle for the method.
        /// </param>
        /// <param name="parameters">
        /// Parameters to pass to method.
        /// </param>
        private static object CallMethod(object obj, DynamicMethodHandle methodHandle, params object[] parameters)
        {
            var method = methodHandle.DynamicMethod;

            object[] inParams = parameters == null ? new object[] { null } : parameters;
            //如果最后一个参数是一个 param 数组,则尝试把参数动态添加到这个数组中。
            if (methodHandle.HasFinalArrayParam)
            {
                bool rebuildParameters = true;

                //四种情况:实参比形参少一个;实参多于形参;
                //实参与形参相等,且最后一个参数是数组的元素;
                //实参与形参相等,且最后一个参数就是数组;
                //其中,只有最后一种情况下,不需要重新构造一个新的参数列表。
                var pCount = methodHandle.MethodParamsLength;
                if (parameters.Length == pCount)
                {
                    var last = parameters[pCount - 1];
                    if (last is Array)
                    {
                        rebuildParameters = false;
                    }
                }

                if (rebuildParameters)
                {
                    //构造一个全新的参数列表集合,并把新集合中除最后一个位置外的元素全部从 imParams 拷贝。
                    object[] newParameterList = new object[pCount];
                    Array.Copy(inParams, newParameterList, pCount - 1);

                    //计算出剩余的所有参数的个数,并构造一个数组,把剩余的所有参数都拷贝进这个数组中。
                    var paramArrayLength = inParams.Length - (pCount - 1);
                    var paramArray = Array.CreateInstance(methodHandle.FinalArrayElementType, paramArrayLength);
                    Array.Copy(inParams, pCount - 1, paramArray, 0, paramArrayLength);
                    newParameterList[newParameterList.Length - 1] = paramArray;

                    //修改为使用新的集合。
                    inParams = newParameterList;
                }
            }

            var result = methodHandle.DynamicMethod(obj, inParams);

            return result;
        }