我有一个程序集A,它定义了一些带有一些重载的接口:
public interface ITransform
{
Point InverseTransform(Point point);
Rect InverseTransform(Rect value);
System.Drawing.Point InverseTransform(System.Drawing.Point point);
}
Run Code Online (Sandbox Code Playgroud)
...和一个引用A(二进制文件而不是项目)的程序集B并调用其中一个重载:
var transform =
(other.Source.TransformToDisplay != null &&
other.Source.TransformToDisplay.Valid) ?
other.Source.TransformToDisplay : null;
if (transform != null)
{
e.Location = transform.InverseTransform(e.Location);
}
Run Code Online (Sandbox Code Playgroud)
准确地说,它调用System.Windows.Point的过载InverseTransform的方法,因为这是属性的类型Location在e.
但是当我在IDE中构建B时,我得到:
错误CS0012:类型'System.Drawing.Point'在未引用的程序集中定义.您必须添加对程序集'System.Drawing,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'的引用.
即使这甚至不是我所说的过载.当我注释掉InverseTransform调用重载方法的行时,即使我仍在实例化类型的对象,它也能正常构建ITransform.
为什么?有没有办法解决这个问题,而无需添加对System.Drawing所有地方的引用?
关于下面提到的编译器错误以及如何解决它,有很多问题/解答,但是这里的问题是询问一些见解,为什么在这种情况下需要这样做。
为什么使用另一个引用的项目B的方法的重载的项目A(在其中一个重载的签名中使用项目C的对象),就要求您引用项目A的项目C,即使您从不使用来自该对象的对象也是如此。项目C?
我想这必须与使用哪种重载有关,但我想了解其背后的概念。
这是一个例子:
将每个类放在自己的程序集中。
//Put into Assembly C
public class C {}
//Put into assembly B, reference C
public class B
{
public static void Test(string param) //Simple method with one string parameter
{
}
public static void Test(C param) //Overload which uses type of assembly C
{
}
}
//Call placed in method of assembly A which uses and references only assembly B, but not C
B.Test("TestString"); // fails to compile, CS0012
Run Code Online (Sandbox Code Playgroud)
CS0012类型'C'在未引用的程序集中定义。您必须添加对程序集'C,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = …
.net c# .net-assembly overload-resolution assembly-references
我的项目引用了一个引用另一个程序集的程序集(将其称为X)(将其称为Y).
当我尝试编译我的项目时,它要求它应该引用程序集Y.为什么会这样?我在引用程序集X的行上收到以下错误:
"DevExpress.XtraEditors.XtraForm"类型在未引用的程序集中定义.您必须添加对程序集'DevExpress.Utils.v9.1,Version = 9.1.2.0,Culture = neutral,PublicKeyToken = b88d1754d700e49a'的引用.
DevExpress.XtraEditors.XtraForm 是集会Y.
为什么是这样?我以前没见过这种行为.