public static object?InvokeCallback(this Callback callback, IReferenceMap referenceMap, IFrameworkToJsiiConverter converter, out string?error) { try { CallbackResult?frameworkResult = callback.InvokeCallbackCore(referenceMap); converter.TryConvert( frameworkResult, referenceMap, frameworkResult?.Value, out object?result ); error = null; return(result); } catch (TargetInvocationException e) { // An exception was thrown by the method being invoked error = e.InnerException?.ToString(); return(null); } catch (Exception e) { // An exception was thrown while preparing the request or processing the result error = e.ToString(); return(null); } }
static object[] ConvertArguments(Parameter[] parameters, params object[] arguments) { IServiceProvider serviceProvider = ServiceContainer.ServiceProvider; if (parameters == null && arguments == null) { return(new object[] { }); } if (parameters == null || arguments == null || parameters.Length != arguments.Length) { throw new ArgumentException("Arguments do not match method parameters", nameof(arguments)); } IFrameworkToJsiiConverter converter = serviceProvider.GetRequiredService <IFrameworkToJsiiConverter>(); IReferenceMap referenceMap = serviceProvider.GetRequiredService <IReferenceMap>(); return(parameters.Zip(arguments, (parameter, frameworkArgument) => { if (!converter.TryConvert(parameter, referenceMap, frameworkArgument, out object jsiiArgument)) { throw new ArgumentException($"Could not convert argument '{frameworkArgument}' to Jsii", nameof(arguments)); } return jsiiArgument; }).ToArray()); }
public static object InvokeCallback(this Callback callback, IReferenceMap referenceMap, IFrameworkToJsiiConverter converter, out string error) { try { object frameworkResult = callback.InvokeCallbackCore(referenceMap); converter.TryConvert( new TypeReference(primitive: PrimitiveType.Any), referenceMap, frameworkResult, out object result ); error = null; return(result); } catch (TargetInvocationException e) { throw e.InnerException; } catch (TargetParameterCountException) { throw; } catch (Exception e) { error = e.ToString(); return(null); } }
static object[] ConvertArguments(Parameter[] parameters, params object[] arguments) { IServiceProvider serviceProvider = ServiceContainer.ServiceProvider; if (parameters == null && arguments == null) { return(new object[] { }); } if (parameters == null || arguments == null || parameters.Length != arguments.Length) { throw new ArgumentException("Arguments do not match method parameters", nameof(arguments)); } var cleanedArgs = new List <object>(arguments); var cleanedParams = new List <Parameter>(parameters); // Handling variadic parameters (null array, empty array, one value array, n values array..) if (parameters.Length > 0 && parameters.Last().IsVariadic) { // Last parameter is variadic, let's explode the .NET attributes Array variadicValues = arguments.Last() as Array; // We remove the last argument (the variadic array); cleanedArgs.RemoveAt(cleanedArgs.Count - 1); // A null value could be passed as a params if (variadicValues != null) { // We save the last parameter to backfill the parameters list var lastParameter = cleanedParams.Last(); for (int i = 0; i < variadicValues.Length; i++) { // Backfill the arguments cleanedArgs.Add(variadicValues.GetValue(i)); // Backfill the parameters if necessary, for a 1:1 mirror with the cleanedArgs if (cleanedArgs.Count != cleanedParams.Count) { cleanedParams.Add(lastParameter); } } } } IFrameworkToJsiiConverter converter = serviceProvider.GetRequiredService <IFrameworkToJsiiConverter>(); IReferenceMap referenceMap = serviceProvider.GetRequiredService <IReferenceMap>(); return(cleanedParams.Zip(cleanedArgs, (parameter, frameworkArgument) => { if (!converter.TryConvert(parameter, referenceMap, frameworkArgument, out object jsiiArgument)) { throw new ArgumentException($"Could not convert argument '{frameworkArgument}' to Jsii", nameof(arguments)); } return jsiiArgument; }).ToArray()); }
static void SetPropertyCore <T>(T value, JsiiPropertyAttribute propertyAttribute, Action <IClient, object> setAction) { IServiceProvider serviceProvider = ServiceContainer.ServiceProvider; IFrameworkToJsiiConverter converter = serviceProvider.GetRequiredService <IFrameworkToJsiiConverter>(); IReferenceMap referenceMap = serviceProvider.GetRequiredService <IReferenceMap>(); if (!converter.TryConvert(propertyAttribute, referenceMap, value, out object jsiiValue)) { throw new ArgumentException($"Could not set property '{propertyAttribute.Name}' to '{value}'", nameof(value)); } IClient client = serviceProvider.GetRequiredService <IClient>(); setAction(client, jsiiValue); }