簡體   English   中英

鑄造 object 到沒有 generics 的類型

[英]Casting object to type without generics

public class ErrorCode
{
    public int HttpStatus { get; private set; }
    public string JsonErrorCode { get; private set; }
    public Type ExceptionT { get; private set; }

    public ErrorCode(string pJsonErrorCode, int pHttpStatus, Type pExceptionType)
    {
        this.HttpStatus = pHttpStatus;
        this.JsonErrorCode = pJsonErrorCode;
    }

    public void ThrowException(string pMsg)
    {
        throw Activator.CreateInstance(ExceptionT, pMsg);
    }
}

因此,如您所見,我正在嘗試創建給定類型的實例。 我不能使用 Generics。 因為我有一個創建錯誤代碼池的字典。

Protocol.ErrorCodes.Add
(
    ErrorCodeType.ElementClickIntercepted,
    new ErrorCode
    (
        "element click intercepted", 
        400, 
        typeof(ElementClickInterceptedException)
    )
);

上面的代碼不起作用,因為正在創建的類型是 object 並且不是從 system.exception 派生的

上面的代碼不適用於 generics 因為字典需要了解正在構建的內容。 我不確定在這里做什么。 我的字典有大約 50 個不同的例外。 想法?

您需要將創建的實例轉換為 Exception

public class ErrorCode
{
  public int HttpStatus { get; private set; }
  public string JsonErrorCode { get; private set; }
  public Type ExceptionT { get; private set; }

  public ErrorCode(string pJsonErrorCode, int pHttpStatus, Type pExceptionType)
  {
    this.HttpStatus = pHttpStatus;
    this.JsonErrorCode = pJsonErrorCode;
    this.ExceptionT = pExceptionType;
  }

  public void ThrowException(string pMsg)
  {
    throw (Exception)Activator.CreateInstance(ExceptionT, pMsg);
  }
}

您可以將Exception替換為ElementClickInterceptedException或任何需要的東西。

測試

var error = new ErrorCode("test", 1, typeof(ArgumentException));
try
{
  error.ThrowException("error");
}
catch ( Exception ex )
{
  Console.WriteLine(ex.Message);
}

小提琴片段

Output

error

我不能使用 Generics。 因為我有一個創建錯誤代碼池的字典。

是的,你真的可以,而且這是正確的解決方案。 您只需要為查找字典聲明一個基本接口。

public interface IErrorCode
{
    int HttpStatus { get; }
    string JsonErrorCode { get; }
    void ThrowException(string pMsg);
}

public class ErrorCode<T> : IErrorCode where T : Exception
{
    public int HttpStatus { get; private set; }
    public string JsonErrorCode { get; private set; }

    public ErrorCode(string pJsonErrorCode, int pHttpStatus)
    {
        this.HttpStatus = pHttpStatus;
        this.JsonErrorCode = pJsonErrorCode;
    }

    public void ThrowException(string pMsg)
    {
        var exception = (T)Activator.CreateInstance
        (
            typeof(T), 
            new object[] { pMsg }
        );
        throw exception;
    }
}


//Declare the dictionary with the interface, not the concrete type
Protocol.ErrorCodes = new Dictionary<ErrorCodeType,IErrorCode>();

Protocol.ErrorCodes.Add
(
    ErrorCodeType.ElementClickIntercepted,
    new ErrorCode<ElementClickInterceptedException>
    (
        "element click intercepted", 
        400
    )
);

如果您需要處理列表中的任何元素,您可以使用該界面——您不必強制轉換任何內容。

獲取 HTTP 狀態:

var status = Protocol.ErrorCodes[key].HttpStatus;

獲取 Json 錯誤代碼:

var errorCode = Protocol.ErrorCodes[key].JsonErrorCode;

通常,您的代碼不應依賴於具體實例,而應依賴於接口(根據SOLID中的“D”)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM