Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I (sometimes) have to reference assemblies referenced by the assembly I reference?

I have an assembly A that defines an interface with some overloads:

public interface ITransform
{
    Point InverseTransform(Point point);
    Rect InverseTransform(Rect value);
    System.Drawing.Point InverseTransform(System.Drawing.Point point);
}

...and an assembly B that references A (the binary, not the project) and calls one of the overloads:

var transform =
    (other.Source.TransformToDisplay != null &&
    other.Source.TransformToDisplay.Valid) ?
    other.Source.TransformToDisplay : null;
if (transform != null)
{
    e.Location = transform.InverseTransform(e.Location);
}

To be precise, it calls the System.Windows.Point overload of the InverseTransform method, because that is the type of the property Location in e.

But when I build B in the IDE I get:

error CS0012: The type 'System.Drawing.Point' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

even though that's not even the overload I am calling. When I comment out the line where the overloaded method InverseTransform is called, it builds fine even though I'm still instantiating an object of type ITransform.

Why? And is there a way to fix this without having to add a reference to System.Drawing everywhere?

like image 937
mtijn Avatar asked Apr 18 '12 13:04

mtijn


2 Answers

The compiler needs to know what a System.Drawing.Point is in order to prove that it's not the correct overload (eg, if it has an implicit conversion).

like image 114
SLaks Avatar answered Sep 20 '22 10:09

SLaks


That method makes use of something defined in System.Drawing. If you uncomment it then that assembly no longer will by trying to use System.Drawing; hence, no requirement.

Think of it this way, when you go off to perform your action .NET says ok I'm making a call to this guy defined in this assembly and looks for the appropriate code to execute. It can't find it so it throws up it's hands and says I give up you tell me where it is.

Just make it a habit of referencing every DLL you might potentially use.

like image 35
scottheckel Avatar answered Sep 21 '22 10:09

scottheckel