简体   繁体   English

为什么我得到:“IThirdParty”类型是在未引用的程序集中定义的。 您必须添加对程序集“ThirdPartyAssembly”的引用吗?

[英]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.在第二个情况下,你暴露IThirdPartyThirdPartyAssembly.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.

相关问题 “组件”类型是在未引用的程序集中定义的。 您必须添加对程序集“System.ComponentModel.Primitives”的引用 - The type 'Component' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel.Primitives' “System.Object”类型在未引用的程序集中定义。 您必须添加对程序集 .netstandard 的引用 - The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard “MarshalByRefObject”类型是在未引用的程序集中定义的。 您必须添加对程序集 &#39;mscorlib, Version=4.0.0.0 的引用 - The type 'MarshalByRefObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0 类型“ System.Data.Common.DbTransaction”在未引用的程序集中定义。 您必须添加对程序集的引用 - The type 'System.Data.Common.DbTransaction' is defined in an assembly that is not referenced. You must add a reference to assembly 类型“ DbConnection”在未引用的程序集中定义。 我无法添加参考? - The type 'DbConnection' is defined in an assembly that is not referenced. I cannot add the reference? NSubstitute -3.x &#39;ValueType&#39; 在未引用的程序集中定义。 您必须添加对程序集“netstandard”的引用, - NSubstitute -3.x 'ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, System.IObserver&#39;1 <T0> 在未引用的程序集中定义。 您必须添加对程序集的引用 - System.IObserver'1<T0> is defined in an assembly that is not referenced. You must add a reference to assembly C#出现错误:在未引用的程序集中未定义类型“ Indirect.Class”。 必须添加对程序集“间接”的引用 - C# Getting Error: Type “Indirect.Class” Not Defined In an Assembly Not Referenced . Must Add a reference to assembly “Indirect” 类型“ IReportServerCredentials”在未引用的程序集中定义。 - type 'IReportServerCredentials' is defined in an assembly that is not referenced. 类型&#39;IAsyncOperationWithProgress &lt;,&gt;&#39;在未引用的程序集中定义。 - The type 'IAsyncOperationWithProgress<,>' is defined in an assembly that is not referenced.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM