返回或继承包含嵌套类的泛型

发布于 2024-10-03 22:12:14 字数 678 浏览 5 评论 0原文

当使用如下所示的类布局时,我注意到一些极其奇怪的行为:

// Contents of assembly A:
public class MyParentClass
{
    public class NestedClass
    {
    }
}

// Contents of assembly B:
public class AnotherClass : List<MyParentClass.NestedClass>
{
}

我可以将程序集 A 添加为程序集 B 的依赖项。此示例将在编译时没有任何错误消息,但是当涉及到加载程序集 B 时,例如作为单元测试的参考,我在编译单元测试时会收到此错误:

无法加载文件或程序集“AssemblyB,版本=1.0.0.0,文化=中性, PublicKeyToken=null' 或其依赖项之一。系统找不到 指定文件。

该代码生成的程序集确实可以工作,我可以使用 Redgate Reflector 运行生成的 DLL,并按预期查看我的代码,但单元测试拒绝加载它。

我几乎肯定这个问题与我调用另一个程序集中的嵌套类有关,而且我可能最终会重构这个设计,但我真的很想知道为什么这不起作用预期的。如果以这种方式使用类出现问题,我希望编译器抛出异常。

谢谢您的宝贵时间。

I am noticing some extremely strange behaviour when using a class layout that looks like this:

// Contents of assembly A:
public class MyParentClass
{
    public class NestedClass
    {
    }
}

// Contents of assembly B:
public class AnotherClass : List<MyParentClass.NestedClass>
{
}

I can add assembly A as a dependency of assembly B. This example will compile without any error messages, but when it comes to, for example, loading assembly B as a reference for unit testing, I will receive this error when it comes time to compile unit tests:

Could not load file or assembly 'AssemblyB, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' or one of its dependencies. The system cannot find the
file specified.

The assembly that this code generates does actually work, and I can run through the generated DLL with Redgate Reflector and see my code as expected, but the unit tests refuse to load it.

I am almost positive that this issue is to do with the fact that I am calling a nested class that is in another assembly, and I will probably end up refactoring this design anyway, but I would really like to know why this does not work as expected. If there was a problem with using the classes in this fashion, I would expect the compiler to throw an exception.

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甩你一脸翔 2024-10-10 22:12:14

是否为该参考启用了“复制本地”?我假设您的测试项目测试程序集 B 没有程序集 A 的副本。在引用上将“复制本地”设置为 true 应该复制程序集,以便在编译(并最终运行)测试时可以正确找到它们。

Is "Copy Local" enabled for that reference? I assume that your test project testing assembly B doesn't have a copy of assembly A. Setting "Copy Local" to true on the references should copy the assemblies, so that they can be found correctly when compiling (and finally running) the tests.

ぺ禁宫浮华殁 2024-10-10 22:12:14

我怀疑这与嵌套类有什么关系——只是 .NET 中缺乏传递依赖关系。

基本上,如果您要在一个项目中使用 AnotherClass,则需要显式添加对 AssemblyB 的引用 程序集A。这样两者都应该被适当地复制和加载。

如果这没有帮助,您可以尝试 启用融合测井

I doubt that this is anything to do with nested classes - just a lack of transitive dependencies being surfaced in .NET.

Basically, if you're going to use AnotherClass in one project, you'll need to explicitly add a reference to both AssemblyB and AssemblyA. That way both should be copied and loaded appropriately.

If that doesn't help, you could try enabling fusion logging.

深陷 2024-10-10 22:12:14

如果您的项目将目标框架设置为“Framework 4 Client Profile”并且链接的 dll 具有目标框架“Framework 4”,您会在 Visual Studio 2010 中收到此错误消息。

在您的项目设置中进行检查。

You get this error message in visual studio 2010 if your project has target framework set to "Framework 4 Client Profile" and the linked dll has a target Framework "Framework 4".

Check that in your project settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文