简体   繁体   中英

Returning or inheriting a generic that contains a nested class

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.

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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