为什么我使用图书馆需要有一个使用语句库B(其中A使用)C#客户端 [英] why does my C# client that uses Library A need to have a using statement for Library B (which A uses)
问题描述
我有:
- 在主程序类 - 使用库
- 在库 - 有部分类,它们组合从图书馆乙 方法
- 在图书馆乙 - 混合的方法和放大器;接口
因此,在图书馆B计算我包括它实现的inode(在库B中定义),我突然得到一个错误在我的主类的地方,它使用节点的图书馆A.错误告诉我在主类我有一个部分节点类到using语句必须库湾
So in Library B when I include a partial Node class which implements INode (defined in Library B) I suddenly get an error in my main class where it uses Node from Library A. The error tells me in the Main Class I have to have a using statement to Library B.
任何想法?
修改 - 除了从code
// *** 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;行是:
So the error that appears against the "Node myNode;" line is:
错误1类型 'Topology.INode`1'中定义 未引用的程序集。 您必须添加一个引用组件 拓扑,版本= 1.0.0.0, 文化=中立, 公钥=空'。 U:\我的 Dropbox的\来源\ ToplogyLibrary \ TopologyDAL_ConsoleTest \ Program.cs的11月13日TopologyDAL_ConsoleTest
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作为参考。那就是有没有使用之前或之后的客户端code是。所以,问题是一个参考不使用的问题。
When I let VS fix it for me it adds the Library2 as a reference. That is there is no "using " either before or after in the client code. So the issue is a Reference not Using issue.
编辑3 - 不专讲这个问题,但我现在发现,在计划项目中,我看不到混入方法(从库B),除非我有一个使用语句库B'我可能会创建一个单独的问题再此。
EDIT 3 - Not specifically about the question, however I now notice that in the program project, I can't see the mixin methods (from Library B) unless I have a using statement to Library B? I might create a separate question re this.
推荐答案
我对所发生的事情的理解是,你只引用库中的主要程序和编译器会告诉你添加引用到库B,因为某些类型的库自曝是在图书馆B中定义的。
My understanding of what's happening is that you only reference Library A from Main program and compiler tells you to add reference to Library B because some types that Library A exposes are defined in Library B.
要解决此引用添加到库B至主程序项目。
To fix this add a reference to Library B to Main program project.
下面是小图。如果库中的暴露在图书馆B中定义一个类型,那么主要应该参考图书馆B来说。以下情况将不工作:
Here is little diagram. If Library A exposes a type defined in Library B then Main must reference Library B as well. The below situation will not work:
_____________ _____________ _____________
| Main |references | Library A |references | Library B |
| -|------------|-> -|--------------|-> |
| | | public | | SomeType |
| | | SomeType | | |
| | | | | |
------------- ------------- -------------
这仅仅是一个问题,当在图书馆B中定义一个类型是通过库A.访问这将在下列情况之一:
This is only an issue when a type defined in Library B is accessible through Library A. This will be in one of the following situations:
- EDITED 在库(
节点
)的类型定义源自于库B(一种的inode&LT; INT&GT;
)。 - 在库中定义的方法使用库B中的类型作为返回类型或者作为参数。
- 在库中定义的类型公开从库B中的类型的属性或公共领域。
- EDITED Type defined in Library A (
Node
) derives from a type in Library B(INode<int>
). - Method defined in Library A uses a type from Library B as return type or as an argument.
- Type defined in Library A exposes a type from Library B as a property or a public field.
您将需要一个参考的Assembly1添加到Assembly3使其编译。
You will need to add a reference to Assembly3 from Assembly1 to make it compile.
_____________ _____________ _____________
| Main |references | Library A |references | Library B |
| -|------------|-> -|--------------|-> |
| | | public | | SomeType |
| |references | SomeType | | |
| -|------------|------------|--------------|-> |
| | | | | |
------------- ------------- -------------
这篇关于为什么我使用图书馆需要有一个使用语句库B(其中A使用)C#客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!