private async Task <StreamingResult> InvokeStreamingMethodAsync(object instance, MethodInfo methodInfo, params object[] parameters) { var ch = await InvokeHelper.InvokeMethodAsync <object>(instance, methodInfo, parameters).ConfigureAwait(false); Type taskType = methodInfo.ReturnType; if (taskType.GetGenericTypeDefinition() == typeof(Task <>)) { taskType = methodInfo.ReturnType.GenericTypeArguments[0]; } var convType = typeof(StreamingResult <>).MakeGenericType(taskType.GenericTypeArguments[0]); var conv = (StreamingResult)Activator.CreateInstance(convType, ch, ClientContext, methodInfo); return(conv); }
private async Task <StreamingResult> InvokeIObservableMethodAsync(object instance, MethodInfo methodInfo, CancellationToken cancellationToken, params object[] parameters) { var ch = await InvokeHelper.InvokeMethodAsync <object>(instance, methodInfo, parameters).ConfigureAwait(false); Type taskType = methodInfo.ReturnType; if (taskType.GetGenericTypeDefinition() == typeof(Task <>)) { taskType = methodInfo.ReturnType.GenericTypeArguments[0]; } var obsGenType = taskType.GenericTypeArguments[0]; // ReSharper disable once PossibleNullReferenceException var convMethod = typeof(ObservableExtensions).GetMethod("AsChannelReaderInternal", BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(obsGenType); var channelReader = convMethod.Invoke(null, new[] { ch, cancellationToken }); var convType = typeof(StreamingResult <>).MakeGenericType(taskType.GenericTypeArguments[0]); var conv = (StreamingResult)Activator.CreateInstance(convType, channelReader, ClientContext, methodInfo); return(conv); }
private async Task <object> InvokeMethodInfoAsync(object instance, MethodInfo methodInfo, IEnumerable <object> arguments, IEnumerable <string> genericArguments, Guid?cancellationTokenGuid) { CancellationToken cancellationToken = default; if (cancellationTokenGuid.HasValue) { var cancellation = new CancellationTokenSource(); cancellationTokenSources.TryAdd(cancellationTokenGuid.Value, cancellation); cancellationToken = cancellation.Token; } var parameters = await BuildExecuteMethodParameters(methodInfo, arguments, cancellationToken); if (genericArguments?.Any() == true) { var arrType = genericArguments.Select(TypeHelper.FindType).ToList(); methodInfo = methodInfo.MakeGenericMethod(arrType.ToArray()); } object result = null; if (methodInfo.ReturnType == typeof(void) || methodInfo.ReturnType == typeof(Task)) { await InvokeHelper.InvokeVoidMethodAsync(instance, methodInfo, parameters); } else { result = await InvokeHelper.InvokeMethodAsync <object>(instance, methodInfo, parameters); } if (cancellationTokenGuid.HasValue) { cancellationTokenSources.TryRemove(cancellationTokenGuid.Value, out var token); } return(result); }
public async Task <object> InvokeMethodInfoAsync(object instance, MethodInfo methodInfo, IEnumerable <object> arguments, IEnumerable <string> genericArguments) { var parameters = BuildExecuteMethodParameters(methodInfo, arguments); SetInvokingInstanceProperties(instance); //var enumerable = genericArguments as string[] ?? genericArguments.ToArray(); if (genericArguments?.Any() == true) { var arrType = genericArguments.Select(TypeHelper.FindType).ToList(); methodInfo = methodInfo.MakeGenericMethod(arrType.ToArray()); } if (methodInfo.ReturnType == typeof(void) || methodInfo.ReturnType == typeof(Task)) { await InvokeHelper.InvokeVoidMethodAsync(instance, methodInfo, parameters); return(null); } else { return(await InvokeHelper.InvokeMethodAsync <object>(instance, methodInfo, parameters)); } }