/// <summary>
        /// Tries to call an instance method with the specified name, a single parameter, and a return value.
        /// </summary>
        /// <typeparam name="TArg1">The type of the method's single parameter.</typeparam>
        /// <typeparam name="TResult">The type of the method's result value.</typeparam>
        /// <param name="source">The object to call the method on.</param>
        /// <param name="methodName">The name of the method to call.</param>
        /// <param name="arg1">The value to pass as the method's single argument.</param>
        /// <param name="value">The value returned by the method.</param>
        /// <returns><c>true</c> if the method was found, <c>false</c> otherwise.</returns>
        public static bool TryCallMethod <TArg1, TResult>(this object source, string methodName, TArg1 arg1, out TResult value)
        {
            var type       = source.GetType();
            var paramType1 = typeof(TArg1);
            var returnType = typeof(TResult);

            object cachedItem = Cache.GetOrAdd(
                new PropertyFetcherCacheKey(type, paramType1, returnType, methodName),
                key =>
                DynamicMethodBuilder <Func <object, TArg1, TResult> >
                .CreateMethodCallDelegate(
                    key.Type1,
                    key.Name,
                    OpCodeValue.Callvirt,
                    methodParameterTypes: new[] { key.Type2 }));

            if (cachedItem is Func <object, TArg1, TResult> func)
            {
                value = func(source, arg1);
                return(true);
            }

            value = default;
            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Tries to call an instance method with the specified name and a return value.
        /// </summary>
        /// <typeparam name="TResult">The type of the method's result value.</typeparam>
        /// <param name="source">The object to call the method on.</param>
        /// <param name="methodName">The name of the method to call.</param>
        /// <param name="value">The value returned by the method.</param>
        /// <returns><c>true</c> if the method was found, <c>false</c> otherwise.</returns>
        public static bool TryCallMethod <TResult>(this object source, string methodName, out TResult value)
        {
            var type = source.GetType();

            object cachedItem = Cache.GetOrAdd(
                new PropertyFetcherCacheKey(type, null, methodName),
                key =>
                DynamicMethodBuilder <Func <object, TResult> >
                .CreateMethodCallDelegate(
                    key.Type1,
                    key.Name,
                    OpCodeValue.Callvirt));

            if (cachedItem is Func <object, TResult> func)
            {
                value = func(source);
                return(true);
            }

            value = default;
            return(false);
        }
        /// <summary>
        /// Tries to call an instance method with the specified name and a return value.
        /// </summary>
        /// <typeparam name="TResult">The type of the method's result value.</typeparam>
        /// <param name="source">The object to call the method on.</param>
        /// <param name="methodName">The name of the method to call.</param>
        /// <param name="value">The value returned by the method.</param>
        /// <returns><c>true</c> if the method was found, <c>false</c> otherwise.</returns>
        public static bool TryCallMethod <TResult>(this object source, string methodName, out TResult value)
        {
            var type = source.GetType();

            object cachedItem = Cache.GetOrAdd(
                $"{type.AssemblyQualifiedName}.{methodName}",
                key =>
                DynamicMethodBuilder <Func <object, TResult> >
                .CreateMethodCallDelegate(
                    type,
                    methodName,
                    OpCodeValue.Callvirt));

            if (cachedItem is Func <object, TResult> func)
            {
                value = func(source);
                return(true);
            }

            value = default;
            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// Tries to call an instance method with the specified name, a single parameter, and a return value.
        /// </summary>
        /// <typeparam name="TArg1">The type of the method's single parameter.</typeparam>
        /// <typeparam name="TResult">The type of the method's result value.</typeparam>
        /// <param name="source">The object to call the method on.</param>
        /// <param name="methodName">The name of the method to call.</param>
        /// <param name="arg1">The value to pass as the method's single argument.</param>
        /// <param name="value">The value returned by the method.</param>
        /// <returns><c>true</c> if the method was found, <c>false</c> otherwise.</returns>
        public static bool TryCallMethod <TArg1, TResult>(this object source, string methodName, TArg1 arg1, out TResult value)
        {
            var type       = source.GetType();
            var paramType1 = typeof(TArg1);

            object cachedItem = Cache.GetOrAdd(
                $"{type.AssemblyQualifiedName}.{methodName}.{paramType1.AssemblyQualifiedName}",
                key =>
                DynamicMethodBuilder <Func <object, TArg1, TResult> >
                .CreateMethodCallDelegate(
                    type,
                    methodName,
                    methodParameterTypes: new[] { paramType1 }));

            if (cachedItem is Func <object, TArg1, TResult> func)
            {
                value = func(source, arg1);
                return(true);
            }

            value = default;
            return(false);
        }
        /// <summary>
        /// Tries to call an instance method with the specified name, two parameters, and no return value.
        /// </summary>
        /// <typeparam name="TArg1">The type of the method's first parameter.</typeparam>
        /// <typeparam name="TArg2">The type of the method's second parameter.</typeparam>
        /// <param name="source">The object to call the method on.</param>
        /// <param name="methodName">The name of the method to call.</param>
        /// <param name="arg1">The value to pass as the method's first argument.</param>
        /// <param name="arg2">The value to pass as the method's second argument.</param>
        /// <returns><c>true</c> if the method was found, <c>false</c> otherwise.</returns>
        public static bool TryCallVoidMethod <TArg1, TArg2>(this object source, string methodName, TArg1 arg1, TArg2 arg2)
        {
            var type       = source.GetType();
            var paramType1 = typeof(TArg1);
            var paramType2 = typeof(TArg2);

            object cachedItem = Cache.GetOrAdd(
                new PropertyFetcherCacheKey(type, paramType1, paramType2, methodName),
                key =>
                DynamicMethodBuilder <Action <object, TArg1, TArg2> >
                .CreateMethodCallDelegate(
                    key.Type1,
                    key.Name,
                    OpCodeValue.Callvirt,
                    methodParameterTypes: new[] { key.Type2, key.Type3 }));

            if (cachedItem is Action <object, TArg1, TArg2> func)
            {
                func(source, arg1, arg2);
                return(true);
            }

            return(false);
        }