-
-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the
RestMethodInfo
available in the request options
While having the interface type available is nice, it might not be enough if using reflection on the called method is desired. Providing the `RestMethodInfo` opens new possibilities to introspect the called method. Currently, to workaround this missing information, I'm using a `methodName` parameter decorated with a `[Property]` attribute and a default value to make it possible to introspect the return type of the called method. At the interface method: ```csharp Task<ApiResponse<User>> GetUserAsync(string id, [Property] string methodName = nameof(GetUserAsync)); ``` Inside the HTTP message handler: ```csharp request.Options.TryGetValue(new HttpRequestOptionsKey<Type>(HttpRequestMessageOptions.InterfaceType), out var refitInterfaceType) request.Options.TryGetValue(new HttpRequestOptionsKey<string>("methodName"), out var methodName) var methodReturnType = refitInterfaceType.GetMethod(methodName).ReturnType; ``` With the new `RestMethodInfo`, it becomes possible to access the method without having to pollute the interface definition: ```csharp request.Options.TryGetValue(HttpRequestMessageOptions.RestMethodInfoKey, out var restMethodInfo); var methodReturnType = restMethodInfo.MethodInfo.ReturnType; ``` Also, the new `HttpRequestMessageOptions.InterfaceTypeKey` and `HttpRequestMessageOptions.RestMethodInfoKey` (available on .NET 5 onwards) make it easier to access the request options.
- Loading branch information
Showing
5 changed files
with
86 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters