tylerjensen/ServiceWire

service method with generic parameters support

oleksabor opened this issue · 1 comments

Hello

Please help me to understand is there a chance to have generic service methods supported?
like

    interface IGenCustomType
    {
        T Load<T>(int id) where T : new();
    }

    class GenCustomType : IGenCustomType
    {
        public T Load<T>(int id) where T : new()
        {
            return new T();
        }
    }

the Host.AddService fails to get method metadata and CreateMethodMap fails to get the MethodSyncInfo.MethodReturnType value

[Fact]
public void AddService()
{
	var host = new Mock<Host>(null);

	var h = host.Object;
	h.AddService<IGenCustomType>(new GenCustomType());
}

I think it may work since MethodSyncInfo.MethodReturnType is used to check is-it-a-Task condition by the StreamingChannel.InvokeMethod only

var returnType = methodSyncInfo.MethodReturnType.ToType();
if (IsTaskType(returnType) && outParams.Length > 0)
{
    if (returnType.IsGenericType)
    {
        MethodInfo methodInfo = typeof(Task).GetMethod(nameof(Task.FromResult))
            .MakeGenericMethod(new[] { returnType.GenericTypeArguments[0] });
        outParams[0] = methodInfo.Invoke(null, new[] { outParams[0] });
	}
    else
    {
        outParams[0] = Task.CompletedTask;
    }
}

so, probably it may be adjusted with if (methodSyncInfo.MethodReturnType != null) check to avoid null reference exception in the methodSyncInfo.MethodReturnType.ToType for T Load<T>(int id) interface method.

Generic parameters are not currently supported.