【问题标题】:C# - Add values from list in a fluent interfaceC# - 在流畅的界面中从列表中添加值
【发布时间】:2018-07-08 19:51:09
【问题描述】:

我是以下代码:

var token = new JwtBuilder()
    .WithAlgorithm(new HMACSHA256Algorithm())
    .WithSecret(_Signature)
    .AddClaim("test", 3)
    .Build();

我想添加多个值(此处称为“声明”):

    .AddClaim("test", 3)
    .AddClaim("test2", 4)
    .AddClaim("test3", 5)

如果我有字典:

    Dictionary<string, int> myDictionary;

如何编写表达式以便添加字典中的所有值? (伪代码:AddClaim(myDictionary))。

编辑: 我正在寻找一种适合“流畅”流程而不是外部循环的方法。

【问题讨论】:

    标签: c# linq fluent


    【解决方案1】:

    您的要求并不完全清楚。如果您想要在您正在使用的 API 中使用的方法,那么这取决于 API 的实现者。例如。也许你在这里使用Jwt.Net,如果你想要“开箱即用”的功能,你必须添加类似的东西。

    否则,您将无法摆脱自己编写循环。但是,您可以将循环封装在扩展方法中:

    static class JwtExtensions
    {
        public static JwtBuilder AddClaims<TKey, TValue>(this JwtBuilder builder, Dictionary<TKey, TValue> dictionary)
        {
            foreach (var kvp in dictionary)
            {
                builder.AddClaim(kvp.Key, kvp.Value);
            }
    
            return builder;
        }
    }
    

    那么你可以这样写:

    var token = new JwtBuilder()
        .WithAlgorithm(new HMACSHA256Algorithm())
        .WithSecret(_Signature)
        .AddClaims(myDictionary)
        .Build();
    

    【讨论】:

      【解决方案2】:

      如果我理解你,你正在寻找扩展 JwtBuilder 类的流畅界面

      我的蜘蛛侠感觉告诉我,没有通过 WithAlgorithm 等其他方法传递的接口,很可能只是 'JwtBuilder` 类本身

      在这种情况下,您可以轻松实现 extension method 来实现您最疯狂的梦想

      public static class JwtBuilder Extensions
      {
          public static JwtBuilder AddClaims(this JwtBuilder source, Dictionary<string, int> claims)
          {
              for (KeyValuePair<string, int> claim in claims) 
              {
                  source.AddClaim(claim .Key, claim .Value);
              }
      
              return source;
          }
      }
      

      注意:你应该检查其他方法的返回类型,因为它可能是一个接口,在这种情况下,只需在你的扩展中使用它我你的扩展方法

      看起来像这样

      public static IInterface AddClaims(this <IInterface> source, Dictionary<string, int> claims)
      
      // Or  
      
      public static T AddClaims<T>(this T source, Dictionary<string, int> claims)
      where T : ISomeInterface
      

      【讨论】:

      • 是的,这就是彼得上面提出的;这就是我要找的!谢谢!
      • @Thomas 抱歉,由于某种原因我没有收到更新,我很适合参加晚会
      【解决方案3】:

      我不确定您是否可以在这里使用循环:

      var tokenBuilder = new JwtBuilder()
          .WithAlgorithm(new HMACSHA256Algorithm())
          .WithSecret(_Signature);
      
      for (KeyValuePair<string, int> entry in myDictionary) {
          tokenBuilder.AddClaim(entry.Key, entry.Value);
      }
      
      var token = tokenBuilder.build();
      

      【讨论】:

      • 我应该在原始问题中明确指出,我正在寻找一种作为流畅表达一部分的方式,而不是用循环“破坏”流程。我将编辑问题
      【解决方案4】:

      你可以使用for循环

      for (KeyValuePair<string, int> claimval in myDictionary) {
          tokenBuilder.AddClaim(claimval.Key, claimval.Value);
      }
      

      【讨论】:

      • 查看上面的评论
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      相关资源
      最近更新 更多