Skip to content

Commit

Permalink
Merge pull request jacqueskang#45 from jacqueskang/generic-support
Browse files Browse the repository at this point in the history
Support generic method
  • Loading branch information
jacqueskang authored Nov 19, 2018
2 parents 890b910 + f85b56b commit 99e246c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/IpcServiceSample.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ private static async Task MainAsync(string[] args)
byte[] reversed = await systemClient.InvokeAsync(x => x.ReverseBytes(input));
Console.WriteLine($"[TEST 7] reversed bytes are: {Convert.ToBase64String(reversed)}");

// test 8: call IPC service method with generic parameter
string print = await systemClient.InvokeAsync(x => x.Printout(DateTime.UtcNow));
Console.WriteLine($"[TEST 8] print out value: {print}");
}
catch (Exception ex)
{
Expand Down
5 changes: 5 additions & 0 deletions src/IpcServiceSample.ConsoleServer/SystemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public Guid GenerateId()
return Guid.NewGuid();
}

public string Printout<T>(T value)
{
return value.ToString();
}

public byte[] ReverseBytes(byte[] input)
{
return input.Reverse().ToArray();
Expand Down
3 changes: 1 addition & 2 deletions src/IpcServiceSample.ServiceContracts/ISystemService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace IpcServiceSample.ServiceContracts
{
Expand All @@ -10,5 +8,6 @@ public interface ISystemService
string ConvertText(string text, TextStyle style);
Guid GenerateId();
byte[] ReverseBytes(byte[] input);
string Printout<T>(T value);
}
}
1 change: 1 addition & 0 deletions src/JKang.IpcServiceFramework.Client/IpcServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private static IpcRequest GetRequest(Expression exp, MyInterceptor interceptor)
{
MethodName = interceptor.LastInvocation.Method.Name,
Parameters = interceptor.LastInvocation.Arguments,
GenericArguments = interceptor.LastInvocation.GenericArguments,
};
}

Expand Down
11 changes: 10 additions & 1 deletion src/JKang.IpcServiceFramework.Core/IpcRequest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
namespace JKang.IpcServiceFramework
using System;

namespace JKang.IpcServiceFramework
{
public class IpcRequest
{
private Type[] _genericArguments = new Type[0];

public string MethodName { get; set; }
public object[] Parameters { get; set; }
public Type[] GenericArguments
{
get => _genericArguments ?? new Type[0];
set => _genericArguments = value;
}
}
}
18 changes: 17 additions & 1 deletion src/JKang.IpcServiceFramework.Server/IpcServiceEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected async Task ProcessAsync(Stream server, ILogger logger, CancellationTok

cancellationToken.ThrowIfCancellationRequested();

logger?.LogDebug($"[thread {Thread.CurrentThread.ManagedThreadId}] request received, invoking corresponding method...");
logger?.LogDebug($"[thread {Thread.CurrentThread.ManagedThreadId}] request received, invoking '{request.MethodName}'...");
IpcResponse response;
using (IServiceScope scope = ServiceProvider.CreateScope())
{
Expand Down Expand Up @@ -95,11 +95,22 @@ protected IpcResponse GetReponse(IpcRequest request, IServiceScope scope)
return IpcResponse.Fail($"Parameter mismatch.");
}

Type[] genericArguments = method.GetGenericArguments();
if (genericArguments.Length != request.GenericArguments.Length)
{
return IpcResponse.Fail($"Generic arguments mismatch.");
}

object[] args = new object[paramInfos.Length];
for (int i = 0; i < args.Length; i++)
{
object origValue = request.Parameters[i];
Type destType = paramInfos[i].ParameterType;
if (destType.IsGenericParameter)
{
destType = request.GenericArguments[destType.GenericParameterPosition];
}

if (_converter.TryConvert(origValue, destType, out object arg))
{
args[i] = arg;
Expand All @@ -112,6 +123,11 @@ protected IpcResponse GetReponse(IpcRequest request, IServiceScope scope)

try
{
if (method.IsGenericMethod)
{
method = method.MakeGenericMethod(request.GenericArguments);
}

object @return = method.Invoke(service, args);
if (@return is Task)
{
Expand Down

0 comments on commit 99e246c

Please sign in to comment.