抛出错误列表时出错?

时间:2011-04-12 04:26:15

标签: c# c#-4.0

在我的项目中,我抛出了错误消息列表

像这样

 List<string> errorMessageList = errors[0].Split(new char[] { ',' }).ToList();

 throw new WorkflowException(errorMessageList);

我的WorkflowException类看起来像这样

/// <summary>
/// WorkFlowException class
/// </summary>
public class WorkFlowException : Exception
{
    /// <summary>
    /// Initializes a new instance of the WorkFlowException class
    /// </summary>
    /// <param name="message">Error Message</param>
    public WorkFlowException(List<string> message)
    {
        base.Message = message;
    }
}

但在将此消息列表分配给base.Message时出错 任何人都可以帮我这样做吗?

3 个答案:

答案 0 :(得分:2)

Exception.Messagestring,而不是List<string>,并且它是只读的,因此您必须通过构造函数链接将string传递给基类:< / p>

public class WorkFlowException : Exception
{
    public WorkFlowException(List<string> messages)
    : base(messages != null && messages.Count > 0 ? messages[0] : "")
    { 
      //...
    }
}

或者,您可以覆盖Message属性:

public class WorkFlowException : Exception
{
    List<string> messages;

    public WorkFlowException(List<string> messages)
    { 
      this.messages = messages
    }

    public override string Message
    {
      get { return messages != null && messages.Count > 0 ? messages[0] : "" }
    }
}

答案 1 :(得分:1)

Exception.Message是一个字符串。您正在尝试将List<string>分配给string媒体资源。 类型不匹配.. 在分配之前,尝试将字符串列表格式化为单个字符串。

更新(谢谢标记)
public WorkflowException(List<string> listOfMessages) : base(String.Join(",", listOfMessages.ToArray());

答案 2 :(得分:0)

Exception.Message是字符串。不是List<string>