gpt4 book ai didi

c# - 为什么 typeof(T) != instance.getType()?

转载 作者:行者123 更新时间:2023-12-03 21:58:05 31 4
gpt4 key购买 nike

我正在开发一个工厂,它将根据类型返回接口(interface)的通用实现。

我的主要问题是由
enter image description here
为什么这些 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;
}
}

附带问题:有没有比使用 object 更好的方法?作为字典值?

在启动时注册处理程序:
var exceptionHandlerFactory = app.ApplicationServices.GetService<IDocumedisExceptionHandlerFactory>();
exceptionHandlerFactory.RegisterExceptionHandler(new FhirParsingExceptionHandler());

在哪里 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;
}
}

处理程序定义(其中 TException 是逆变的):
public interface IDocumedisExceptionHandler<in TException>
where TException : DocumedisException
{
void HandleException(TException exception, out HttpStatusCode httpStatusCode, out OperationOutcome.IssueType issueType, out string message);
}

FhirParsingException扩展 DocumedisException :
public class FhirParsingException : DocumedisException
{
[...]
}

从中间件中检索处理程序:
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);
[...]
}

最佳答案

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();
}

现在你有:
Animal animal = new Turtle();
Feed(animal);

放心, CheckTypes将返回 false ;泛型类型参数的类型是 Animal但是 animal 的运行时类型真的是 Turtle .

关于c# - 为什么 typeof(T) != instance.getType()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61103002/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com