Aaron Thomas
Aaron Thomas

Reputation: 5281

Why must an assembly be referenced twice?

Question

It seems to me that any time I make a class library, all of its dependencies should just come with it. In other words, I would expect to just reference a .dll and go. After all, the .dll I reference builds just fine on its own.

Is this not the case? I reference all of a dependency's dependencies, in order to use it?

Looking forward to being enlightened about this.

Issue

To illustrate, here's an example.

Up to this point, everything builds. Everything is the same framework.

However, when I attempt to initiate ClassLibrary2.Class1, I get a build error:

Error 1 The type 'ClassLibrary1.Class1' is defined in an assembly that is not referenced. You must add a reference to assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. ...\ConsoleApplication1\Program.cs 12 4 ConsoleApplication1

Code Snippets

namespace ClassLibrary1
{
    public class Class1
    {
        public Class1() { }
    }
}

...

namespace ClassLibrary2
{
    public class Class1 : ClassLibrary1.Class1
    {
        public Class1() : base() { }
    }
}

...

namespace ConsoleApplication1
{
    using ClassLibrary2;

    class Program
    {
        static void Main(string[] args)
        {
            // error mentioned previously in post is on following line
            var a = new Class1();
        }
    }
}

Upvotes: 3

Views: 501

Answers (2)

Julian Wilson
Julian Wilson

Reputation: 2470

I'll try to formulate an answer here. It helps to look at dependencies on the project level in VS and hopefully it will be more clear.

Suppose you have two projects, ProjectA and ProjectB in a solution. Both projects reference an assembly Important.dll. By reference here, I am referring to if you right click -> Add 'Reference' in a VS project. Suppose also, that ProjectA references ProjectB in our solution in VS. By this I mean, right click -> 'Add Reference' -> 'Solution' -> Pick Project B

What is actually going on? Remember, all a VS reference is a way to help the compiler find the .dll. At build time (compiling), VS will go through all the projects in the solution that are required to be built (you can see this in the Configuration Manager). It will notice that both ProjectA and ProjectB are set to build. Then it will look at all its direct dependencies in the references section by traversing the dependency tree. For all the VS references that are set to 'Copy local' (true by default), they will be sent to the build folder. So Important.dll will go to the build folder.. you probably knew that.

But ProjectA references ProjectB in VS. When you have a project that references another project in a solution, all you're really doing is pointing to the built .dll of that project... in this case ProjectB.dll. It's no different from antoher .dll. You can see this if you look at the references section under 'Path' in ProjectA... it'll be something like C:\users\jdwqdwqd\vs\ProjectB\ProjectB\bin\x64\ProjectB.dll. So this will be copied to the output folder as well.

I guess one might ask 'Won't Important.dll be copied twice along with ProjectB.dll?. VS gives you a lot of help here and will pick the correct .dll to copy.

Hope this helps.

Some more information:

https://msdn.microsoft.com/en-us/library/ez524kew.aspx

Upvotes: 3

Uchiha Itachi
Uchiha Itachi

Reputation: 1271

It's just the way C# works. The inheritance rules seem to be skewed. I'm working on a massive project at work, and I can use class libraries and namespaces across multiple files, but I have to reference them every time, regardless of inheritance. It's weird and annoying. Maybe they'll fix that soon.

Upvotes: 0

Related Questions