c# - c#中未知异常类型的泛型类
问题描述
在我的应用程序Web API
中,我有一个异常过滤器,它假设捕获任何异常并将其传输到响应包装类中的客户端。我的目标是使该异常以正确的类型传输,以便客户端可以重建并重新抛出它。
以下是异常捕获器的代码:
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
HttpStatusCode OutputHttpCode = HttpStatusCode.InternalServerError;
var exceptionType = actionExecutedContext.Exception.GetType();
if (exceptionType == typeof(InvalidIDException))
{
OutputHttpCode = HttpStatusCode.Unauthorized;
}
//this way of getting type didnt work for me either
//var exceptionType = typeof(RestErrorResponse<>).MakeGenericType(actionExecutedContext.Exception.GetType());
actionExecutedContext.Response = new HttpResponseMessage()
{
Content = new StringContent(JsonConvert.SerializeObject(
//this will not compile here, saying t is a variable but used like a type.
//If i use generic "Exception" instead everything is working fine,
//but it will be interpreted as generic exception on client side and
//could not be handled properly if i rethrow it directly
new RestErrorResponse<exceptionType> () //Exception will compile
{
Content = null,
Status = RestStatus.Error,
Exception = actionExecutedContext.Exception
}
),
System.Text.Encoding.UTF8, "application/json"),
StatusCode = OutputHttpCode
};
base.OnException(actionExecutedContext);
}
这是一个带有泛型的类,我试图将我的异常放入:
public class RestErrorResponse<E> :RestResponse<Object> {
public E myException { get; set; }
}
如果我在“RestErrorResponse”类中使用通用异常,这是一个创建的 JSON:
{
"Exception": {
"ClassName": "InvalidLoginException",
"Message": "Invalid User Name",
"Data": {},
"InnerException": null,
"HelpURL": null,
"StackTraceString": "....",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": "....",
"HResult": -2147024809,
"Source": "DB",
"WatsonBuckets": null,
"ParamName": null
},
"Status": {
"Verbal": "Error",
"Code": 1074
},
"Content": null
}
我的目标是获得:
{
"InvalidLoginException": {
"ClassName": "InvalidLoginException",
"Message": "Invalid User Name",
"Data": {},
"InnerException": null,
"HelpURL": null,
"StackTraceString": "....",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": "....",
"HResult": -2147024809,
"Source": "DB",
"WatsonBuckets": null,
"ParamName": null
},
"Status": {
"Verbal": "Error",
"Code": 1074
},
"Content": null
}
解决方案
RestErrorResponse 是您控制的类吗?如果是这样,它真的必须是通用的吗?考虑一下:
public class RestErrorResponse : RestResponse<Object> {
public object Exception { get; set; }
}
现在您可以简单地实例化它:
var response = new RestErrorResponse
{
// other properties
Exception = actionExecutedContext.Exception
}
如果无法更改,则需要反射来创建 RestErrorResponse 的实例。例如:
var responseType = typeof(RestErrorResponse<>).MakeGenericType(exceptionType);
dynamic response = Activator.CreateIntance(type);
response.Content = null;
response.Status = RestStatus.Error;
response.Exception = actionExecutedContext.Exception;