Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="methodInfo"></param>
        /// <param name="ownerInstance"></param>
        /// <param name="paramObjs"></param>
        /// <returns></returns>
        public static object InvokeMethod(MethodInfo methodInfo, object ownerInstance, object[] paramObjs)
        {
            DynamicMethodHandler handler = null;

            StringBuilder sb = new StringBuilder();

            sb.Append(methodInfo.DeclaringType.FullName);
            sb.Append("|");
            sb.Append(methodInfo.Name);

            Type[] genericParameterTypes = null;
            if (methodInfo.IsGenericMethod)
            {
                genericParameterTypes = methodInfo.GetGenericArguments();
            }

            if (genericParameterTypes != null)
            {
                for (int i = 0; i < genericParameterTypes.Length; ++i)
                {
                    sb.Append("|");
                    sb.Append(genericParameterTypes[i].ToString());
                }
            }

            string key = sb.ToString();

#if Server
            handler = CacheDynamicMethod.GetOrAdd(key, new Func <string, DynamicMethodHandler>((s) => CreatMethodDelegate(methodInfo, genericParameterTypes)));
#else
            lock (CacheDynamicMethod)
            {
                if (CacheDynamicMethod.ContainsKey(key))
                {
                    handler = CacheDynamicMethod[key];
                }
                else
                {
                    handler = CreatMethodDelegate(methodInfo, genericParameterTypes);
                    CacheDynamicMethod.Add(key, handler);
                }
            }
#endif
            //调用相应的方法
            return(handler(ownerInstance, paramObjs));
        }
Exemplo n.º 2
0
 protected virtual object Invoke(T target, DynamicMethodHandler <T> handler, object[] args)
 {
     return(handler.Invoke(target, args));
 }