5.43The HttpDeclarativeBuilder Builder (Dynamic Building)

Created on Aug 17, 2026~3 min read

The HttpDeclarativeBuilder builder is provided by the framework specifically for dynamically building the various settings required by HTTP declarative requests. The constructor of HttpDeclarativeBuilder is private, so it cannot be instantiated directly with the new keyword; however, the framework provides multiple static overloads of HttpRequestBuilder.Declarative to create HttpDeclarativeBuilder instances.

cs
HttpRequestBuilder.Declarative(methodInfo, args);

The code above demonstrates how to dynamically build an HTTP declarative request builder using a MethodInfo typed parameter and an argument array. This mechanism enables us to implement HTTP declarative request functionality for methods of any type. The following is a concrete example:

cs
public class NormalClass{    [Get("https://furion.net"/)]    public Task<string> GetStringAsync()    {        throw new NotImplementedException();    // No implementation needed    }}

Through the following steps, we can dynamically build an HTTP declarative request based on the GetStringAsync method of NormalClass:

cs
// Get the GetStringAsync method of the NormalClass typevar getStringMethod = typeof(NormalClass).GetMethod(nameof(NormalClass.GetStringAsync), BindingFlags.Instance | BindingFlags.Public);// Send the HTTP requestvar str = await httpRemoteService.SendAsAsync<string>(HttpRequestBuilder.Declarative(getStringMethod, []));

In this way, we implement the dynamic building of HTTP declarative requests for methods of any type. In addition, HTTP declarative requests support a variety of methods, including but not limited to:

cs
httpRemoteService.Declarative(method, args);await httpRemoteService.DeclarativeAsync<T>(method, args);httpRemoteService.SendAs(httpDeclarativeBuilder);await httpRemoteService.SendAsAsync<T>(httpDeclarativeBuilder);