Phi*_*ppe 4 c# generics casting
我正在开发一个工厂,该工厂将根据类型返回接口的通用实现。
我的主要问题正在说明
为什么这些 typeof(TException) != exception.GetType()?分别地,我必须更改什么才能拥有正确的 TException 类型?
上面的代码导致 InvalidCast 异常,因为它试图转换为 IDocumedisExceptionHandler<DocumedisException>而不是IDocumedisExceptionHandler<FhirParsingException>
工厂实施:
internal class DocumedisExceptionHandlerFactory : IDocumedisExceptionHandlerFactory
{
private readonly IDictionary<Type, object> _exceptionHandlers = new ConcurrentDictionary<Type, object>();
public void RegisterExceptionHandler<TException>(IDocumedisExceptionHandler<TException> exceptionHandler)
where TException : DocumedisException
{
_exceptionHandlers.Add(typeof(TException), exceptionHandler);
}
public IDocumedisExceptionHandler<TException> GetDocumedisExceptionHandler<TException>(TException exception)
where TException : DocumedisException
{
_exceptionHandlers.TryGetValue(exception.GetType(), out var exceptionHandler);
return (IDocumedisExceptionHandler<TException>) exceptionHandler;
}
}
Run Code Online (Sandbox Code Playgroud)
附带问题:有没有比object用作字典值更好的方法?
在启动时注册处理程序:
var exceptionHandlerFactory = app.ApplicationServices.GetService<IDocumedisExceptionHandlerFactory>();
exceptionHandlerFactory.RegisterExceptionHandler(new FhirParsingExceptionHandler());
Run Code Online (Sandbox Code Playgroud)
哪里FhirParsingExceptionHandler实施IDocumedisExceptionHandler
internal class FhirParsingExceptionHandler : IDocumedisExceptionHandler<FhirParsingException>
{
public void HandleException(FhirParsingException exception, out HttpStatusCode httpStatusCode, out OperationOutcome.IssueType issueType, out string message)
{
httpStatusCode = HttpStatusCode.BadRequest;
issueType = OperationOutcome.IssueType.Invalid;
message = exception.Message;
}
}
Run Code Online (Sandbox Code Playgroud)
处理程序定义(其中TException是逆变的):
public interface IDocumedisExceptionHandler<in TException>
where TException : DocumedisException
{
void HandleException(TException exception, out HttpStatusCode httpStatusCode, out OperationOutcome.IssueType issueType, out string message);
}
Run Code Online (Sandbox Code Playgroud)
并FhirParsingException扩展DocumedisException:
public class FhirParsingException : DocumedisException
{
[...]
}
Run Code Online (Sandbox Code Playgroud)
从中间件检索处理程序:
public async Task Invoke(HttpContext context)
{
try
{
await _next.Invoke(context);
}
catch (Exception ex)
{
if (ex is DocumedisException documedisException)
{
await HandleDocumedisExceptionAsync(context, documedisException);
}
else
{
throw;
}
}
}
private async Task HandleDocumedisExceptionAsync<TException>(HttpContext context, TException ex, MedicationAnalyzerErrorCode? errorCode = null)
where TException : DocumedisException
{
var exceptionHandler = _documedisExceptionHandlerFactory.GetDocumedisExceptionHandler(ex);
[...]
}
Run Code Online (Sandbox Code Playgroud)
typeof(TException)为您提供了编译时的类型exception。exception.GetType()为您提供了运行时的类型exception。这两者根本不需要相同,编译器所做的唯一保证是运行时类型exception可分配给TException变量。
考虑以下:
class Animal { }
class Turtle: Animal { }
bool CheckTypes<T>(T animal) where T: Animal
{
return typeof(T) == animal.GetType();
}
Run Code Online (Sandbox Code Playgroud)
现在你有:
Animal animal = new Turtle();
Feed(animal);
Run Code Online (Sandbox Code Playgroud)
放心,CheckTypes会回来的false;泛型类型参数的类型是,Animal但运行时类型animal是真的Turtle。