【问题标题】:How to store multiple value types in a property?如何在一个属性中存储多个值类型?
【发布时间】:2013-10-04 00:34:51
【问题描述】:

我正在创建一个事件类,目前如下所示:

public class SharePointOnErrorEventsArgs : EventArgs
{
    public SharePointOnErrorEventsArgs(string message, bool showException, Exception exception)
    {
        Message = message;
        Exception = exception;
        ShowException = showException;
    }

    /// <summary>
    /// Property to allow the storage of a more verbose and explainable error message
    /// </summary>
    public string Message { get; private set; }

    /// <summary>
    /// Object to store full exception information within
    /// </summary>
    public Exception Exception { get; private set; }

    /// <summary>
    /// Boolean value allows for verbose messages to be sent up the stack without
    /// the need for displaying a full exception object, or stack trace.
    /// </summary>
    public bool ShowException { get; private set; }
}

现在,我想发送DebugInfoError 三个值之一,而不是发送truefalseshowException - 我该如何解决这样的问题?我真的不想使用字符串,因为我想始终将其限制为这三个值之一,但我不确定在使用属性时如何处理。

【问题讨论】:

  • 你要找的是enum

标签: c# .net enums


【解决方案1】:

你可以使用枚举:

public enum ShowExceptionLevel
{
    Debug,
    Info,
    Error
}

所以你的班级将是:

public class SharePointOnErrorEventsArgs : EventArgs
{

    public enum ShowExceptionLevel
    {
        Debug,
        Info,
        Error
     }

    public SharePointOnErrorEventsArgs(string message, ShowExceptionLevel showExceptionLevel, Exception exception)
    {
        Message = message;
        Exception = exception;
        ShowException = showException;
    }

    /// <summary>
    /// Property to allow the storage of a more verbose and explainable error message
    /// </summary>
    public string Message { get; private set; }

    /// <summary>
    /// Object to store full exception information within
    /// </summary>
    public Exception Exception { get; private set; }

    /// <summary>
    /// Boolean value allows for verbose messages to be sent up the stack without
    /// the need for displaying a full exception object, or stack trace.
    /// </summary>
    public ShowExceptionLevel ShowException { get; private set; }
}

【讨论】:

  • 谢谢您-我现在会阅读这些内容。我会尽快接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多