繁体   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