使用ASP.Net的任何人都可能看到接受Action<T>用于配置某些选项的委托的方法.以ASP.Net Core MVC Startup.cs为例.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication().AddJwtBearer(options => {
options.Audience = "something";
// ...
});
//...
}
Run Code Online (Sandbox Code Playgroud)
该 .AddJwtBearer()方法在lambda表达式中使用配置参数的Action<T>委托,我正在尝试做的是在我的一个项目中复制它,但没有运气.我得到了部分,但我似乎无法检索调用方法时配置的结果对象.Method(Action<T> action)
上下文的一些代码:
构建器类方法:
public ReporterBuilder SetEmail(Action<EmailConfig> config)
{
if (config is null)
throw new ArgumentNullException();
// Get configured EmailConfig somehow...
return this;
}
Run Code Online (Sandbox Code Playgroud)
EmailConfig模型:
public class EmailConfig
{
public EmailProvider EmailProvider { get; set; }
public string Host { get; set; }
public int Port { get; set; }
public …Run Code Online (Sandbox Code Playgroud)