c# - 为什么我使用库 A 的 C# 客户端需要有库 B(A 使用)的 using 语句

标签 c# .net interface

我有:

  • 主程序类 - 使用库 A
  • 库 A - 有部分类混合了库 B 的方法
  • 库 B - 混合方法和接口(interface)

所以在库 B 中,当我包含一个实现 INode(在库 B 中定义)的部分节点类时,我突然在我的主类中遇到一个错误,它使用库 A 中的节点。该错误告诉我在主类中我有对库 B 有一个 using 语句。

有什么想法吗?

编辑 - 代码除外

    // *** PROGRAM ***
    class Program
    {
        static void Main(string[] args)
        {
            var context = new Model1Container();
            Node myNode;  // ** WITHOUT A using for Library B I have an error here ***
         }
     }


// ** LIBRARY A
namespace TopologyDAL
{
    public partial class Node
    {
        // Auto generated from EF
    }

    public partial class Node : INode<int>   // to add extension methods from Library B
    {
        public int Key
    }
}

// ** LIBRARY B
namespace ToplogyLibrary
{
    public static class NodeExtns
    {
        public static void FromNodeMixin<T>(this INode<T> node) {
           // XXXX
        }
    }
    public interface INode<T> 
    {
        // Properties
        T Key { get; }

        // Methods
    }

编辑 2 - 澄清它是引用还是使用错误:

因此针对“节点 myNode;”出现的错误行是:

Error 1 The type 'Topology.INode`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'Topology, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. U:\My Dropbox\source\ToplogyLibrary\TopologyDAL_ConsoleTest\Program.cs 11 13 TopologyDAL_ConsoleTest

当我让 VS 为我修复它时,它会添加 Library2 作为引用。那就是在客户端代码之前或之后都没有“使用”。所以问题是 Reference not Using 问题。

编辑 3 - 不是特别针对这个问题,但是我现在注意到在程序项目中,除非我有一个 using 语句到库 B,否则我看不到 mixin 方法(来自库 B)?我可能会为此创建一个单独的问题。

最佳答案

我对发生的事情的理解是,您仅从主程序引用库 A,编译器告诉您添加对库 B 的引用,因为库 A 公开的某些类型在库 B 中定义。

要解决此问题,请将对库 B 的引用添加到主程序项目中。

这是小图。如果库 A 公开了库 B 中定义的类型,那么 Main 也必须引用库 B。以下情况将不起作用:

_____________             _____________               _____________
| Main       |references  | Library A  |references    | Library B  |
|           -|------------|->         -|--------------|->          |
|            |            | public     |              | SomeType   |
|            |            |  SomeType  |              |            |
|            |            |            |              |            |
-------------             -------------               -------------

只有在库 B 中定义的类型可通过库 A 访问时才会出现此问题。这将出现在以下情况之一:

  • 已编辑 库 A ( Node ) 中定义的类型派生自库 B ( INode<int> ) 中的类型。
  • 库 A 中定义的方法使用库 B 中的类型作为返回类型或参数。
  • 库 A 中定义的类型将库 B 中的类型公开为属性或公共(public)字段。

您需要从 Assembly1 添加对 Assembly3 的引用以使其编译。

_____________             _____________               _____________
| Main       |references  | Library A  |references    | Library B  |
|           -|------------|->         -|--------------|->          |
|            |            | public     |              | SomeType   |
|            |references  |  SomeType  |              |            |
|           -|------------|------------|--------------|->          |
|            |            |            |              |            |
-------------             -------------               -------------

关于c# - 为什么我使用库 A 的 C# 客户端需要有库 B(A 使用)的 using 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33624181/

相关文章:

c# - 字典 .Values 和 .Keys 到数组(顺序相同)

c# POST json 没有按预期工作?

.net - 什么会导致程序集构建版本和运行时版本不同?

c# - .Net 发布生成的 exe 的内容是什么?

.net - 相同的 If() 和 If 产生不同的结果

language-agnostic - "program to an interface"是什么意思?

Java:断言( boolean 表达式)

arrays - 具有不同输入的 systemverilog 接口(interface)数组

c# - 删除机器人后如何将其添加到联系人列表

c# - 带有嵌入式 list 的 C# 4.0 应用程序是否需要在客户端计算机上安装数字证书或 Stong 名称?