Пример #1
0
 public void OnMethodCall(MethodCall call, IResult result)
 {
     if (call.Method == "increment")
     {
         var counter = (Java.Lang.Integer)call.Arguments();
         result.Success(new Java.Lang.Integer(counter.IntValue() + 1));
     }
     else
     {
         result.NotImplemented();
     }
 }
        private void BackgroundHandleMethodCall(FlutnetMethodInfo methodInfo, MethodCall call)
        {
            FlutnetRuntime.OperationInfo operation;
            try
            {
                operation = FlutnetRuntime.GetOperation(methodInfo.Instance, methodInfo.Operation);
            }
            catch (Exception)
            {
                SendError(methodInfo, new FlutnetException(FlutnetErrorCode.OperationNotImplemented));
                return;
            }

            if (operation.Parameters.Length > 0 && call.Arguments() == null)
            {
                SendError(methodInfo, new FlutnetException(FlutnetErrorCode.OperationArgumentCountMismatch));
                return;
            }

            object[] arguments = new object[operation.Parameters.Length];
            try
            {
                for (int i = 0; i < operation.Parameters.Length; i++)
                {
                    ParameterInfo param     = operation.Parameters[i];
                    Type          paramType = param.IsOut || param.ParameterType.IsByRef
                        ? param.ParameterType.GetElementType()
                        : param.ParameterType;
                    string paramName = param.Name.FirstCharUpper();

                    object value;
                    if (call.HasArgument(paramName))
                    {
                        Object argumentValue = call.Argument(paramName);

                        string serializedArg = argumentValue.ToString();

                        // Deserialize the argument
                        value = JsonConvert.DeserializeObject(serializedArg, paramType, FlutterInterop.JsonSerializerSettings);
                    }
                    else if (param.HasDefaultValue)
                    {
                        value = param.DefaultValue;
                    }
                    else
                    {
                        SendError(methodInfo, new FlutnetException(FlutnetErrorCode.InvalidOperationArguments));
                        return;
                    }

                    arguments[i] = value;
                }
            }
            catch (Exception)
            {
                SendError(methodInfo, new FlutnetException(FlutnetErrorCode.OperationArgumentParsingError));
                return;
            }

            PlatformOperationResult result = PlatformOperationRunner.Run(operation, arguments);

            if (result.Error != null)
            {
                if (result.Error is PlatformOperationException flutterException)
                {
                    SendError(methodInfo, flutterException);
                }
                else
                {
                    //In case of an unhandled exception, send to Flutter a verbose error message for better diagnostic
                    FlutnetException error = new FlutnetException(FlutnetErrorCode.OperationFailed, result.Error.ToStringCleared(), result.Error);
                    SendError(methodInfo, error);
                }
            }
            else
            {
                SendResult(methodInfo, result.Result);
            }
        }