简体   繁体   English

没有C的参考项目B

[英]Reference project B without C

I have three projects A, B and C. These are connected together like so: 我有三个项目A,B和C。它们像这样连接在一起:

A -> B -> C A-> B-> C

If project C has a base like so: 如果项目C具有这样的基础:

public class CBaseClass
{
    public string Name { get; set; }
}

and project B has an inheriting class like so: 项目B具有如下继承类:

public class BInheritingClass : CBaseClass
{

}

How can I access BInheritingClass.Name in project A without it giving me the following error: 如何在项目A中访问BInheritingClass.Name而不给我以下错误:

The type 'CBaseClass' is defined in an assembly that is not referenced. 类型“ CBaseClass”是在未引用的程序集中定义的。 You must add a reference to assembly 'ProjectC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. 您必须添加对程序集'ProjectC,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'的引用。

when I do not have A referencing C. 当我没有A引用C时。

To clarify I want project A to reference B that uses things from C without A referencing C. 为了澄清,我希望项目A引用B,该B使用C中的内容,而A不引用C。

Is this possible? 这可能吗?

You won't be able to avoid referencing C, because CBaseClass is part of the public interface of BInheritingClass . 您将无法避免引用C,因为CBaseClassCBaseClass的公共接口的BInheritingClass Project A has no way of knowing the full definition of BInheritingClass without knowing CBaseClass . BInheritingClass不知道CBaseClass项目A无法知道CBaseClass的完整定义。

If you want to avoid this, you have to avoid inheritance and use composition - make BInheritedClass not inherit CBaseClass , and instead contain it internally, not exposing it as a parameter or public member. 如果要避免这种情况,则必须避免继承并使用组合-使BInheritedClass 继承CBaseClass ,而是在内部包含它,而不是将其公开为参数或公共成员。 Only that way can it remain an implementation detail of B, not part of its contract. 只有这样,它才能保留B的实现细节,而不是其合同的一部分。

If project A in any way uses anything defined public in project C , you need to reference C . 如果项目A以任何方式使用了项目C定义的公共内容,则需要引用C In your example, project A defines a class based on something defined in C, so you need to reference C. 在您的示例中,项目A基于C中定义的内容定义了一个类,因此您需要引用C。


You updated your question 您更新了问题
This answer is still true: As B defines a BInheritingClass based on CBaseClass defined in C , A needs to know C if it wants to use the class in B , otherwise it would not know the "base features" of CBaseClass and thus can not determine the features of BInheritingClass . 这个答案依然是事实:当B定义BInheritingClass基于CBaseClass定义CA需要知道C如果想使用类B ,否则将不知道的“基本功能” CBaseClass ,因此不能确定BInheritingClass的功能。

If you don't want A to "know" C , you may not expose anything from C through B to A . 如果您不希望A “知道” C ,则可能不会暴露任何从CB A Composition is one solution to the problem, but this essentially means you need to remodel the public interface of CBaseClass in BNoLongerInheritingClass . 组合是解决该问题的一种方法,但这实际上意味着您需要在BNoLongerInheritingClass重塑CBaseClass的公共接口。

No, you can't use a class defined in an assembly you did not reference. 不,您不能使用未引用的程序集中定义的类。 (Except for dynamic loading with reflection). (除具有反射的动态加载外)。 Maintaining this reference hierarchy is exactly why tools like NuGet where developed. 维护此参考层次结构正是开发NuGet之类的工具的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM