【问题标题】:IList<string> passed to IEnumerable parameterIList<string> 传递给 IEnumerable 参数
【发布时间】:2013-10-17 08:48:04
【问题描述】:

今天早上我的大脑完全不工作,所以我希望有人能帮我解决这个问题。

我有一个小助手方法可以检查 IEnumerable 是否为空或没有项目。

public static class ParameterGuard
{
    public static void ThrowIfNullOrEmtpy<T>(T enumerable, string argName) where T : IEnumerable
    {
        if (enumerable == null)
            throw new ArgumentNullException(argName);
        if (enumerable.HasCountOf(0))
            throw new ArgumentException(Resources.ExceptionEnumerableEmpty, argName);
    }

    public static void ThrowIfNullOrEmpty(string arg, string argName)
    {
        if (arg == null) 
            throw new ArgumentNullException(argName);
        if (arg.Length == 0) 
            throw new ArgumentException(Resources.ExceptionStringEmpty, argName);
    }
}

我还有一个用于发送电子邮件的小数据结构

public class EmailOptions
{
    public string Server { get; set; }
    public int Port { get; set; }
    public string From { get; set; }
    public List<string> To { get; set; }
    public List<string> Cc { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public bool IsHtml { get; set; }

}

当我尝试使用 ThrowIfNullOrEmpty 方法验证 To 属性时,出现异常。

  private MailMessage CreateEmail(EmailOptions options)
  {
        ParameterGuard.ThrowIfNullOrEmpty(options.To, "To");
        ...
  }

例外

The best overloaded method match for 
ParameterGuard.ThrowIfNullOrEmpty(string, string)' has some invalid arguments

我原以为IList<> 类实现了 IEnumerable,这会起作用。

如果有人帮我解决这个问题,我将不胜感激。

【问题讨论】:

  • 如果是复制/粘贴代码,可能是因为第一个函数调用的是ThrowIfNullOrEmtpy,而不是ThrowIfNullOrEmpty
  • 你确定吗?我们无法重现问题
  • @SWeko ;你说得对。这是一个错字。您要创建并回答积分吗
  • 这个问题似乎离题了,因为它过于本地化(有一个不明显的错字)

标签: c# .net list generics ienumerable


【解决方案1】:

我已经尝试了代码,如果这两个方法确实是重载,则(正确)使用了通用版本。

在粘贴的代码中,第一个函数被称为ThrowIfNullOrEmtpy,而不是ThrowIfNullOrEmpty,所以这是一个简单的拼写错误。


此 LINQPad 代码:

void Main()
{
  CreateEmail(new EmailOptions());
}

private void CreateEmail(EmailOptions options)
{
  ParameterGuard.ThrowIfNullOrEmpty(options.To, "To");
}

public static class ParameterGuard
{
      public static void ThrowIfNullOrEmpty<T>(T enumerable, string argName) 
        where T : IEnumerable
      {
          Console.Write("Generic Version");
      }

      public static void ThrowIfNullOrEmpty(string arg, string argName)
      {
          Console.Write("String Version");
      }
}

public class EmailOptions
{
    public List<string> To { get; set; }
}

返回“通用版本”

【讨论】:

  • 谢谢...慢慢地把头撞在桌子上:-)
  • @PhilMurray,为什么要使用两种方法? string 还实现了IEnumerable(和 IEnumerable)!
【解决方案2】:

不要像您使用的那样使用通用方法,而是使用普通方法:

public static void ThrowIfNullOrEmtpy(IEnumerable enumerable, string argName);

泛型不是必需的。

其次。这些方法有不同的名称。这可能只是一个错字:

ThrowIfNullOrEmtpyThrowIfNullOrEmpty

【讨论】:

  • 我已经按照上面的方法改了,但是还是没有编译,同样的异常
  • @PhilMurray 您尝试修正拼写吗?
【解决方案3】:

你为什么不试试这个:

public static void ThrowIfNullOrEmpty<T>(IEnumerable<T> enumerable, string argName)
{
    ...
}

如上所述,您的第一个方法名称会出错。

【讨论】:

    【解决方案4】:

    string也实现了IEnumerable,只能使用一种方法:

    public static class ParameterGuard
    {
        public static void ThrowIfNullOrEmpty<T>(IEnumerable<T> enumerable, string argName)
        {
            if (enumerable == null)
                throw new ArgumentNullException(argName);
            if (!enumerable.Any())
                throw new ArgumentException();
        }
    }
    
    public class Program
    {
    
        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            list.Add("test");
            ParameterGuard.ThrowIfNullOrEmpty(list, "list");
            ParameterGuard.ThrowIfNullOrEmpty("string", "str");
        }
    }
    

    【讨论】:

      【解决方案5】:

      如下更改您的方法签名:

      public static class ParameterGuard
      {
          public static void ThrowIfNullOrEmpty(IEnumerable enumerable, string argName)
          {
          }
      
          public static void ThrowIfNullOrEmpty(string arg, string argName)
          {
          }
      }
      

      您的两个方法有一个小错字,这可能是造成混乱的原因。这样您应该使用可枚举的方法调用该方法。

      【讨论】:

        猜你喜欢
        • 2012-12-18
        • 2015-11-20
        • 1970-01-01
        • 2010-10-16
        • 1970-01-01
        • 2014-04-06
        • 1970-01-01
        • 2014-03-10
        • 2013-04-09
        相关资源
        最近更新 更多