Matthias Güntert
Matthias Güntert

Reputation: 4638

Why do I get "The type is defined in an assembly that is not referenced" in this case?

Consider the following situation which results in:

The type 'ConnectionStringSettingsCollection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Configuration, Version ...'.

Assembly A.dll:

// References System.Configuration

public class Foo
{
    public Foo(int value)
    {
        // ...
    }

    // ConnectionStringSettingsCollection is defined in System.Configuration
    public Foo(ConnectionStringSettingsCollection connString)  
    { 
        // ...
    }
}

Assembly B.dll:

// References A.dll and _not_ System.Configuration

public class Bar
{
    void SomeMethod()
    {
        var aFoo = new Foo(3); // Complains
    }
}

The line var aFoo = new Foo(3); complains with the error message mentioned above, which is clear and understandable.

However I don't understand, why I have to reference System.Configuration in assembly B.dll when the type isn't publicly exposing any property nor has any method which returns anything of that type.

Upvotes: 1

Views: 248

Answers (1)

Ondrej Tucny
Ondrej Tucny

Reputation: 27962

the type isn't publicly exposing any property nor has any method which returns anything of that type

This is not really true. The Foo type has a public constructor which expects that type as its argument:

public Foo(ConnectionStringSettingsCollection connString) 

hence it is part of the type's (and the assembly's) public contract. In the B assembly the compiler needs to understand this contract in full, so that it is able to resolve and call the right methods, properties, constructors etc.

Upvotes: 4

Related Questions