【问题标题】:Shorthand conditional in C# similar to SQL 'in' keywordC# 中类似于 SQL 'in' 关键字的简写条件
【发布时间】:2010-09-07 04:06:27
【问题描述】:

在 C# 中是否有一种简写方式:

public static bool IsAllowed(int userID)
{
    return (userID == Personnel.JohnDoe || userID == Personnel.JaneDoe ...);
}

喜欢:

public static bool IsAllowed(int userID)
{
    return (userID in Personnel.JohnDoe, Personnel.JaneDoe ...);
}

我知道我也可以使用 switch,但是我必须编写大约 50 个这样的函数(将经典的 ASP 站点移植到 ASP.NET),所以我希望它们尽可能短。

【问题讨论】:

    标签: c# lambda conditional if-statement


    【解决方案1】:

    我会将允许的 ID 列表封装为 data 而不是 code。然后它的源可以在以后轻松更改。

    List<int> allowedIDs = ...;
    
    public bool IsAllowed(int userID)
    {
        return allowedIDs.Contains(userID);
    }
    

    如果使用 .NET 3.5,您可以使用 IEnumerable 而不是 List,这要归功于扩展方法。

    (此函数不应是静态的。请参阅此帖子:using too much static bad or good ?。)

    【讨论】:

      【解决方案2】:

      这样的事情怎么样:

      public static bool IsAllowed(int userID) {
        List<int> IDs = new List<string> { 1,2,3,4,5 };
        return IDs.Contains(userID);
      }
      

      (您当然可以更改静态状态,在其他地方初始化 IDs 类,使用 IEnumerable 等,根据您的需要。要点是最接近 in 运算符是 Collection.Contains() 函数。)

      【讨论】:

        【解决方案3】:

        权限是否基于用户 ID?如果是这样,您可能会通过基于角色的权限获得更好的解决方案。或者您最终可能不得不频繁地编辑该方法以将其他用户添加到“允许的用户”列表中。

        例如, 枚举用户角色 { 用户、管理员、LordEmperor }

        class User {
            public UserRole Role{get; set;}
            public string Name {get; set;}
            public int UserId {get; set;}
        }
        
        public static bool IsAllowed(User user) {
            return user.Role == UserRole.LordEmperor;
        }
        

        【讨论】:

        • 我很乐意这样做,但不幸的是,我们对遗留代码的了解太深了。
        【解决方案4】:

        一个不错的小技巧是反转你通常使用 .Contains() 的方式,例如:-

        public static bool IsAllowed(int userID) {
          return new int[] { Personnel.JaneDoe, Personnel.JohnDoe }.Contains(userID);
        }
        

        您可以在数组中放置任意数量的条目。

        如果 Personnel.x 是一个枚举,你会遇到一些转换问题(以及你发布的原始代码),在这种情况下它会更容易使用:-

        public static bool IsAllowed(int userID) {
          return Enum.IsDefined(typeof(Personnel), userID);
        }
        

        【讨论】:

          【解决方案5】:

          这是我能想到的最接近的:

          using System.Linq;
          public static bool IsAllowed(int userID)
          {
            return new Personnel[]
                { Personnel.JohnDoe, Personnel.JaneDoe }.Contains((Personnel)userID);
          }
          

          【讨论】:

            【解决方案6】:

            只是另一个语法想法:

            return new [] { Personnel.JohnDoe, Personnel.JaneDoe }.Contains(userID);
            

            【讨论】:

              【解决方案7】:

              你能为 Personnel 写一个迭代器吗?

              public static bool IsAllowed(int userID)
              {
                  return (Personnel.Contains(userID))
              }
              
              public bool Contains(int userID) : extends Personnel (i think that is how it is written)
              {
                  foreach (int id in Personnel)
                      if (id == userid)
                          return true;
                  return false;
              }
              

              【讨论】:

                【解决方案8】:

                这个怎么样?

                public static class Extensions
                {
                    public static bool In<T>(this T testValue, params T[] values)
                    {
                        return values.Contains(testValue);
                    }
                }
                

                用法:

                Personnel userId = Personnel.JohnDoe;
                
                if (userId.In(Personnel.JohnDoe, Personnel.JaneDoe))
                {
                    // Do something
                }
                

                我不能为此声称功劳,但我也不记得我在哪里看到的。所以,感谢你,匿名的互联网陌生人。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2012-07-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-04-01
                  • 1970-01-01
                  • 2020-04-23
                  相关资源
                  最近更新 更多