[英]Why am I getting: The type 'IThirdParty' is defined in an assembly that is not referenced. You must add a reference to assembly 'ThirdPartyAssembly'?
Suppose there is third party assembly ThirdPartyAssembly.dll
that expose following:假设有第三方程序集
ThirdPartyAssembly.dll
公开以下内容:
namespace ThirdPartyAssembly
{
public interface IThirdParty
{
void GetInstance(ThirdPartyInfo info);
}
public class ThirdPartyInfo
{
public ThirdPartyInfo(string instanceText);
public string InstanceText { get; set; }
}
}
At one of the solution's project MyAssembly
I refer ThirdPartyAssembly.dll
and implement following code:在解决方案的项目
MyAssembly
之一中,我引用了ThirdPartyAssembly.dll
并实现了以下代码:
namespace MyAssembly
{
public abstract class AbstractMyClass1 : IThirdParty
{
void IThirdParty.GetInstance(ThirdPartyInfo info)
{
info.InstanceText = "some-text";
}
}
public abstract class AbstractMyClass1Consumer<T>
where T : AbstractMyClass1
{
}
}
At second solution project MyAssemblyConsumer
I refer MyAssembly
(as solution project reference) and implement following classed在第二个解决方案项目
MyAssemblyConsumer
我参考MyAssembly
(作为解决方案项目参考)并实现以下分类
namespace MyAssemblyConsumer
{
class MyClass1 : AbstractMyClass1
{
}
class MyClass1Consumer : AbstractMyClass1Consumer<MyClass1>
{
}
}
Everything compiles fine so far.到目前为止,一切都编译得很好。 However, when I add
IMyClass2
into MyAssembly
project that inherits IThirdParty
interface with following abstract classes但是,当我将
IMyClass2
添加到MyAssembly
项目时,该项目继承了具有以下抽象类的IThirdParty
接口
namespace MyAssembly
{
public interface IMyClass2 : IThirdParty
{
}
public abstract class AbstractMyClass2 : IMyClass2
{
void IThirdParty.GetInstance(ThirdPartyInfo info)
{
info.InstanceText = "some-text";
}
}
public abstract class AbstractMyClass2Consumer<T>
where T : IMyClass2
{
}
}
And try to implement following classes into MyAssemblyConsumer
并尝试在
MyAssemblyConsumer
实现以下类
namespace MyAssemblyConsumer
{
class MyClass2 : AbstractMyClass2
{
}
class MyClass2Consumer : AbstractMyClass2Consumer<MyClass2>
{
}
}
I'm getting following compilation error on MyClass2Consumer
:我在
MyClass2Consumer
上遇到以下编译错误:
The type 'IThirdParty' is defined in an assembly that is not referenced. “IThirdParty”类型是在未引用的程序集中定义的。 You must add a reference to assembly 'ThirdPartyAssembly'
您必须添加对程序集“ThirdPartyAssembly”的引用
The question why I don't need to refer ThirdParty.dll at first case, but need this reference in the second case?问题为什么我不需要在第一种情况下引用 ThirdParty.dll,但在第二种情况下需要这个引用?
This is happening because in first case you "hide" your reference to ThirdPartyAssembly.dll
.发生这种情况是因为在第一种情况下,您“隐藏”了您对
ThirdPartyAssembly.dll
的引用。 Yes, your public AbstractMyClass1
implements IThirdParty
from it, but it implements it implicitly so the only way to call IThirdParty.GetInstance()
method is like this:是的,您的公共
AbstractMyClass1
IThirdParty
实现了IThirdParty
,但它隐式地实现了它,因此调用IThirdParty.GetInstance()
方法的唯一方法是这样的:
var myClass1Instance = new MyClass1();
var info = new ThirdPartyInfo();
(myClass1Instance as IThirdParty).GetInstance(info); // this can be called
myClass1Instance.GetInstance(info); // <- this method doesn't exists
So at compile time of your MyAssemblyConsumer
project compiler doesn't need to know anything about IThirdParty
.因此,在
MyAssemblyConsumer
项目编译器的编译时,不需要了解有关IThirdParty
任何信息。 As you said your first case is compiling successfully, I suppose you don't have any code like this.正如您所说,您的第一个案例编译成功,我想您没有这样的代码。
In your second case you expose IThirdParty
of ThirdPartyAssembly.dll
by your public IMyClass2
interface.在第二个情况下,你暴露
IThirdParty
的ThirdPartyAssembly.dll
通过您的公共IMyClass2
接口。 In this case compiler must know about IThirdParty
interface at compile time (on that line where you defining AbstractMyClass2Consumer<T>
), that's why you get this exception.在这种情况下,编译器必须在编译时知道
IThirdParty
接口(在您定义AbstractMyClass2Consumer<T>
那一行),这就是您收到此异常的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.