为什么我(有时)必须引用我引用的程序集所引用的程序集? [英] Why do I (sometimes) have to reference assemblies referenced by the assembly I reference?
问题描述
我有一个程序集 A,它定义了一个带有一些重载的接口:
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);
}
准确地说,它调用了InverseTransform
方法的System.Windows.Point
重载,因为这是Location
属性的类型> 在 e
中.
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 时,我得到:
But when I build B in the IDE I get:
错误 CS0012:类型System.Drawing.Point"是在未引用的程序集中定义的.您必须添加对程序集System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=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屋!