小编Cri*_*aru的帖子

C#使用lambda表达式将参数传递给方法

使用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)

c# .net-core

1
推荐指数
1
解决办法
933
查看次数

标签 统计

.net-core ×1

c# ×1