Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Dec 15, 2023
1 parent 19b6a4e commit 0c984c0
Showing 1 changed file with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,50 @@ static string GetRequestHandlerMethod(Type handlerType, Type? requestType, Type

static LanguageServerEndpointAttribute? GetMethodAttributeFromHandlerMethod(Type handlerType, Type? requestType, Type contextType, Type? responseType)
{
var methodInfo = (requestType != null, responseType != null) switch
const string handleRequestName = nameof(IRequestHandler<object, object, object>.HandleRequestAsync);
const string handleNotificationName = nameof(INotificationHandler<object, object>.HandleNotificationAsync);

foreach (var methodInfo in handlerType.GetRuntimeMethods())
{
(true, true) => handlerType.GetMethod(nameof(IRequestHandler<object, object, object>.HandleRequestAsync), new Type[] { requestType!, contextType, typeof(CancellationToken) }),
(false, true) => handlerType.GetMethod(nameof(IRequestHandler<object, object>.HandleRequestAsync), new Type[] { contextType, typeof(CancellationToken) }),
(true, false) => handlerType.GetMethod(nameof(INotificationHandler<object, object>.HandleNotificationAsync), new Type[] { requestType!, contextType, typeof(CancellationToken) }),
(false, false) => handlerType.GetMethod(nameof(INotificationHandler<object>.HandleNotificationAsync), new Type[] { contextType, typeof(CancellationToken) })
};
if (MethodInfoMatches(methodInfo))
return methodInfo.GetCustomAttribute<LanguageServerEndpointAttribute>();
}

throw new InvalidOperationException("Somehow we are missing the method for our registered handler");

if (methodInfo is null)
bool MethodInfoMatches(MethodInfo methodInfo)
{
throw new InvalidOperationException("Somehow we are missing the method for our registered handler");
switch (requestType != null, responseType != null)
{
case (true, true):
return (methodInfo.Name == handleRequestName || methodInfo.Name.EndsWith("." + handleRequestName)) &&
TypesMatch(methodInfo, [requestType!, contextType, typeof(CancellationToken)]);
case (false, true):
return (methodInfo.Name == handleRequestName || methodInfo.Name.EndsWith("." + handleRequestName)) &&
TypesMatch(methodInfo, [contextType, typeof(CancellationToken)]);
case (true, false):
return (methodInfo.Name == handleNotificationName || methodInfo.Name.EndsWith("." + handleNotificationName)) &&
TypesMatch(methodInfo, [requestType!, contextType, typeof(CancellationToken)]);
case (false, false):
return (methodInfo.Name == handleNotificationName || methodInfo.Name.EndsWith("." + handleNotificationName)) &&
TypesMatch(methodInfo, [contextType, typeof(CancellationToken)]);
}
}

return methodInfo.GetCustomAttribute<LanguageServerEndpointAttribute>();
bool TypesMatch(MethodInfo methodInfo, Type[] types)
{
var parameters = methodInfo.GetParameters();
if (parameters.Length != types.Length)
return false;

for (int i = 0, n = parameters.Length; i < n; i++)
{
if (!Equals(types[i], parameters[i].ParameterType))
return false;
}

return true;
}
}

static LanguageServerEndpointAttribute? GetMethodAttributeFromClassOrInterface(Type type)
Expand Down

0 comments on commit 0c984c0

Please sign in to comment.