Esempio n. 1
0
        private static DynamicMethodHandle GetCachedMethod(object obj, System.Reflection.MethodInfo info, params object[] parameters)
        {
            var key = new MethodCacheKey(obj.GetType().FullName, info.Name, GetParameterTypes(parameters));
            DynamicMethodHandle mh = null;
            var found = false;

            try
            {
                found = _methodCache.TryGetValue(key, out mh);
            }
            catch
            { /* failure will drop into !found block */ }
            if (!found)
            {
                lock (_methodCache)
                {
                    if (!_methodCache.TryGetValue(key, out mh))
                    {
                        mh = new DynamicMethodHandle(info, parameters);
                        _methodCache.Add(key, mh);
                    }
                }
            }
            return(mh);
        }
        private static DynamicMethodHandle GetCachedMethod(object obj, System.Reflection.MethodInfo info, params object[] parameters)
        {
            var key = new MethodCacheKey(obj.GetType().FullName, info.Name, GetParameterTypes(parameters));
            DynamicMethodHandle mh = null;

            if (!_methodCache.TryGetValue(key, out mh))
            {
                lock (_methodCache)
                {
                    if (!_methodCache.TryGetValue(key, out mh))
                    {
                        mh = new DynamicMethodHandle(info, parameters);
                        _methodCache.Add(key, mh);
                    }
                }
            }
            return(mh);
        }
Esempio n. 3
0
        private static DynamicMethodHandle GetCachedMethod(object obj, string method, bool hasParameters, params object[] parameters)
        {
            var key = new MethodCacheKey(obj.GetType().FullName, method, GetParameterTypes(hasParameters, parameters));
            DynamicMethodHandle mh = null;

            if (!_methodCache.TryGetValue(key, out mh))
            {
                lock (_methodCache)
                {
                    if (!_methodCache.TryGetValue(key, out mh))
                    {
                        var info = GetMethod(obj.GetType(), method, hasParameters, parameters);
                        mh = new DynamicMethodHandle(info, parameters);
                        _methodCache.Add(key, mh);
                    }
                }
            }
            return(mh);
        }
Esempio n. 4
0
 private static DynamicMethodHandle GetCachedMethod(object obj, System.Reflection.MethodInfo info, params object[] parameters)
 {
   var key = new MethodCacheKey(obj.GetType().FullName, info.Name, GetParameterTypes(parameters));
   DynamicMethodHandle mh = null;
   var found = false;
   try
   {
     found = _methodCache.TryGetValue(key, out mh);
   }
   catch
   { /* failure will drop into !found block */ }
   if (!found)
   {
     lock (_methodCache)
     {
       if (!_methodCache.TryGetValue(key, out mh))
       {
         mh = new DynamicMethodHandle(info, parameters);
         _methodCache.Add(key, mh);
       }
     }
   }
   return mh;
 }
Esempio n. 5
0
 private static DynamicMethodHandle GetCachedMethod(object obj, string method, bool hasParameters, params object[] parameters)
 {
   var key = new MethodCacheKey(obj.GetType().FullName, method, GetParameterTypes(hasParameters, parameters));
   DynamicMethodHandle mh = null;
   if (!_methodCache.TryGetValue(key, out mh))
   {
     lock (_methodCache)
     {
       if (!_methodCache.TryGetValue(key, out mh))
       {
         var info = GetMethod(obj.GetType(), method, hasParameters, parameters);
         mh = new DynamicMethodHandle(info, parameters);
         _methodCache.Add(key, mh);
       }
     }
   }
   return mh;
 }
Esempio n. 6
0
    private static object CallMethod(object obj, DynamicMethodHandle methodHandle, bool hasParameters, params object[] parameters)
    {
      object result = null;
      var method = methodHandle.DynamicMethod;

      object[] inParams = null;
      if (parameters == null)
        inParams = new object[] { null };
      else
        inParams = parameters;

      if (methodHandle.HasFinalArrayParam)
      {
        // last param is a param array or only param is an array
        var pCount = methodHandle.MethodParamsLength;
        var inCount = inParams.Length;
        if (inCount == pCount - 1)
        {
          // no paramter was supplied for the param array
          // copy items into new array with last entry null
          object[] paramList = new object[pCount];
          for (var pos = 0; pos <= pCount - 2; pos++)
            paramList[pos] = parameters[pos];
          paramList[paramList.Length - 1] = hasParameters && inParams.Length == 0 ? inParams : null;

          // use new array
          inParams = paramList;
        }
        else if ((inCount == pCount && inParams[inCount - 1] != null && !inParams[inCount - 1].GetType().IsArray) || inCount > pCount)
        {
          // 1 or more params go in the param array
          // copy extras into an array
          var extras = inParams.Length - (pCount - 1);
          object[] extraArray = GetExtrasArray(extras, methodHandle.FinalArrayElementType);
          Array.Copy(inParams, pCount - 1, extraArray, 0, extras);

          // copy items into new array
          object[] paramList = new object[pCount];
          for (var pos = 0; pos <= pCount - 2; pos++)
            paramList[pos] = parameters[pos];
          paramList[paramList.Length - 1] = extraArray;

          // use new array
          inParams = paramList;
        }
      }
      try
      {
        result = methodHandle.DynamicMethod(obj, inParams);
      }
      catch (Exception ex)
      {
        throw new CallMethodException(obj.GetType().Name + "." + methodHandle.MethodName + " " + Resources.MethodCallFailed, ex);
      }
      return result;
    }
Esempio n. 7
0
        private static object CallMethod(object obj, DynamicMethodHandle methodHandle, bool hasParameters, params object[] parameters)
        {
            object result = null;
            var    method = methodHandle.DynamicMethod;

            object[] inParams = null;
            if (parameters == null)
            {
                inParams = new object[] { null }
            }
            ;
            else
            {
                inParams = parameters;
            }

            if (methodHandle.HasFinalArrayParam)
            {
                // last param is a param array or only param is an array
                var pCount  = methodHandle.MethodParamsLength;
                var inCount = inParams.Length;
                if (inCount == pCount - 1)
                {
                    // no paramter was supplied for the param array
                    // copy items into new array with last entry null
                    object[] paramList = new object[pCount];
                    for (var pos = 0; pos <= pCount - 2; pos++)
                    {
                        paramList[pos] = parameters[pos];
                    }
                    paramList[paramList.Length - 1] = hasParameters && inParams.Length == 0 ? inParams : null;

                    // use new array
                    inParams = paramList;
                }
                else if ((inCount == pCount && inParams[inCount - 1] != null && !inParams[inCount - 1].GetType().IsArray) || inCount > pCount)
                {
                    // 1 or more params go in the param array
                    // copy extras into an array
                    var      extras     = inParams.Length - (pCount - 1);
                    object[] extraArray = GetExtrasArray(extras, methodHandle.FinalArrayElementType);
                    Array.Copy(inParams, pCount - 1, extraArray, 0, extras);

                    // copy items into new array
                    object[] paramList = new object[pCount];
                    for (var pos = 0; pos <= pCount - 2; pos++)
                    {
                        paramList[pos] = parameters[pos];
                    }
                    paramList[paramList.Length - 1] = extraArray;

                    // use new array
                    inParams = paramList;
                }
            }
            try
            {
                result = methodHandle.DynamicMethod(obj, inParams);
            }
            catch (Exception ex)
            {
                throw new CallMethodException(obj.GetType().Name + "." + methodHandle.MethodName + " " + Resources.MethodCallFailed, ex);
            }
            return(result);
        }