/// <summary>
        /// Check that the method is valid for <see cref="ReactMethodAttribute"/>.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="attribute">The attribute.</param>
        public void Validate(MethodInfo method, ReactMethodAttribute attribute)
        {
            var returnType = method.ReturnType;

            if (!attribute.IsBlockingSynchronousMethod && returnType != typeof(void))
            {
                throw new NotSupportedException("Native module methods must either return void or Task.");
            }

            var parameters = method.GetParameters();
            var n          = parameters.Length;

            for (var i = 0; i < n; ++i)
            {
                var parameterType = parameters[i].ParameterType;
                if (parameterType == typeof(IPromise) && i != (n - 1))
                {
                    throw new NotSupportedException("Promises are only supported as the last parameter of a native module method.");
                }
                else if (parameterType == typeof(ICallback) && i != (n - 1))
                {
                    if (i != (n - 2) || parameters[n - 1].ParameterType != typeof(ICallback))
                    {
                        throw new NotSupportedException("Callbacks are only supported in the last two positions of a native module method.");
                    }
                }
            }
        }
Ejemplo n.º 2
0
            public NativeMethod(NativeModuleBase instance, MethodInfo method, ReactMethodAttribute attribute)
            {
                var delegateFactory = instance._delegateFactory;

                delegateFactory.Validate(method, attribute);
                _invokeDelegate = new Lazy <Func <IReactInstance, JArray, JToken> >(() => delegateFactory.Create(instance, method));
                Type            = delegateFactory.GetMethodType(method, attribute);
            }
        /// <summary>
        /// Extracts the native method type from the method.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="attribute">The attribute.</param>
        /// <returns>The native method type.</returns>
        public string GetMethodType(MethodInfo method, ReactMethodAttribute attribute)
        {
            if (attribute.IsBlockingSynchronousMethod)
            {
                return(SyncMethodType);
            }

            var parameters = method.GetParameters();

            if (parameters.Length > 0 && parameters.Last().ParameterType == typeof(IPromise))
            {
                return(PromiseMethodType);
            }

            return(AsyncMethodType);
        }
        /// <summary>
        /// Extracts the native method type from the method.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="attribute">The attribute.</param>
        /// <returns>The native method type.</returns>
        public string GetMethodType(MethodInfo method, ReactMethodAttribute attribute)
        {
            if (attribute.IsBlockingSynchronousMethod)
            {
                return(SyncMethodType);
            }

            if (method.ReturnType == typeof(Task))
            {
                throw new NotImplementedException("Async methods are not yet supported.");
            }

            var parameters = method.GetParameters();

            if (parameters.Length > 0 && parameters.Last().ParameterType == typeof(IPromise))
            {
                return(PromiseMethodType);
            }

            return(AsyncMethodType);
        }