为什么我(有时候)必须引用我所引用的程序集引用的程序? [英] 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);
}
...并引用A(二进制,而不是项目),并调用重载之一的程序集B:
...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);
}
要为precise,它调用 InverseTransform
方法的 System.Windows.Point
过载,因为那是财产位置
在电子
的类型。
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
.
但是,当我在IDE中建立B I得到:
But when I build B in the IDE I get:
错误CS0012:类型'System.Drawing.Point'在未引用的程序集定义。您必须添加一个引用到组装'System.Drawing中,版本= 4.0.0.0,文化=中性公钥= b03f5f7f11d50a3a'。
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'.
尽管这甚至不是我呼吁过载。当我注释掉,其中重载方法 InverseTransform
被称为线,它建立精细即使我仍然实例类型的对象 ITransform
。
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
.
为什么呢?而且是有办法解决这个问题,而无需添加引用 System.Drawing中
无处不在?
Why? And is there a way to fix this without having to add a reference to System.Drawing
everywhere?
推荐答案
编译器需要知道什么是 System.Drawing.Point
是为了证明这不是正确的过载(例如,如果它有一个隐式转换)。
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).
这篇关于为什么我(有时候)必须引用我所引用的程序集引用的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!