简体   繁体   English

从基类继承时的引用规则

[英]Rules for references when inheriting from base class

Given that Project A is a C# Class Library and that Project B is a console application that has a dependency on Project A. 鉴于项目A是C#类库,而项目B是一个依赖于项目A的控制台应用程序。

In Project A the following class is defined: 在项目A中定义了以下类:

public class ActionMailNotifier : RazorMailerBase
{
        private readonly string _viewPath;
        private readonly EmailHost _emailConfig;
                   ...
                   ...
}

"RazorMailBase" is contained in an external dll, ActionMailer.dll, that is referenced by Project A. “RazorMailBase”包含在项目A引用的外部DLL ActionMailer.dll中。

In Project B the following class is defined: 在项目B中定义了以下类:

public class EmailFareMonitorAlertNotifier : ActionMailNotifier
{
                        ...
                        ...
}

If ActionMailer.dll is not referenced in Project B the compiler generates an error message indicating that a reference to ActionMailer.dll is required. 如果Project B中未引用ActionMailer.dll,编译器将生成一条错误消息,指示需要对ActionMailer.dll的引用。 Is there a way to structure this such that a reference to the external dll is not required in Project B? 有没有办法构建它,以便在项目B中不需要引用外部dll?

EmailFareMonitorAlertNotifier只要EmailFareMonitorAlertNotifier最终从程序集external.dll派生,您需要引用它,以便编译器和运行时具有使用该类型所需的信息。

No. If you were to remove all references to any type in Project B that references ActionMailer.dll, you wouldn't see any errors. 不。如果您要删除项目B中引用ActionMailer.dll的任何类型的所有引用,您将看不到任何错误。 But since RazorMailBase is defined in ActionMailer.dll, you'll have to have that reference to use ActionMailNotifier , or any other type derived from a class in the external library. 但由于RazorMailBase是在ActionMailer.dll中定义的,因此您必须具有使用ActionMailNotifier引用,或者从外部库中的类派生的任何其他类型。

作为绝对最小值,项目B将必须引用其自己类型的EmailFareMonitorAlertNotifier的继承层次结构中存在的所有程序集,所以我猜答案是否定的。

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

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