简体   繁体   English

单元测试中动态转换失败的通用解决方案

[英]generic resolution with dynamic cast failing in Unit Test

I'm working on a .NET Core 5 application.我正在开发 .NET Core 5 应用程序。 In some parts of the system, I'm using a cast to dynamic to handle generic type resolution.在系统的某些部分,我使用强制转换为dynamic来处理泛型类型解析。 Something like this:像这样的东西:

public void Foo(ISomething something){
    Bar((dynamic)something);
}

private void Bar<T>(T somethingElse) where T : ISomething
{ .... }

Works pretty well if I run the whole application.如果我运行整个应用程序,效果会很好。 Now, for some mysterious reason, the same code refuses to work during Unit Tests.现在,由于某种神秘的原因,相同的代码在单元测试期间拒绝工作。 I get an exception as soon as it tries to call Bar<T>() , complaining that it's receiving an object instead of an ISomething .一旦它尝试调用Bar<T>() ,我就会收到一个异常,抱怨它收到的是object而不是ISomething I'm using XUnit.我正在使用 XUnit。

Any idea?任何想法?

UPDATE 10/01/2021 I've pushed the code to GitHub , the branch is coverage .更新 10/01/2021我已将代码推送到 GitHub ,分支是coverage

This is the failing test. 这是失败的测试。 The issue happens on this line in the InMemoryPublisher class, when casting the message to dynamic.将消息转换为动态时,问题发生在InMemoryPublisher class 中的这一行

ok, I found the issue.好的,我发现了问题。 I had marked the ISomething implementations as internal .我已将ISomething实现标记为internal Switching to public fixed it.切换到public修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM