简体   繁体   English

简单继承在项目之间不起作用

[英]Simple inheritance does not work between projects

I just know I'm being an idiot, so somebody please tell me how. 我只知道我是个白痴,所以有人请告诉我怎么做。

Setup is simple: 设置很简单:

  1. Create a solution with three projects (.Net framework, class libraries) named InherTest , InherTest.Base , and InherTest.Base.Inherited 创建一个名为InherTestInherTest.BaseInherTest.Base.Inherited三个项目(.Net框架,类库)的解决方案

  2. In InherTest.Base , add the following class: InherTest.Base ,添加以下类:

    namespace InherTest.Base { public abstract class BaseClass { internal abstract string MEMBER_1 { get; 名称空间InherTest.Base {公共抽象类BaseClass {内部抽象字符串MEMBER_1 {get; } } } }}}

  3. Copy the exact same code into InherTest , including the namespace. 将完全相同的代码(包括名称空间)复制到InherTest

  4. In InherTest.Base.Inherited , add the following class: InherTest.Base.Inherited ,添加以下类:

    namespace InherTest.Base.Inherited { public class Inherited : BaseClass { internal override string MEMBER_1 { get; 名称空间InherTest.Base.Inherited {公共类Inherited:BaseClass {内部重写字符串MEMBER_1 {get; } } } }}}

  5. Add a project reference in InherTest.Base.Inherited to InherTest.Base . InherTest.Base.Inherited InherTest.Base项目引用添加到InherTest.Base Note the errors ("abstract member not implemented" and "no suitable member found to override"). 注意错误(“未实现抽象成员”和“未找到要覆盖的合适成员”)。

  6. Remove that reference and replace it with one to InherTest . 删除该引用,并将其替换为InherTest Observe that the inheritance also fails . 观察到继承也失败

Why don't they both work? 他们为什么都不起作用?

Edit: Originally I stated that one test would fail and the other would succeed; 编辑:最初我说过一个测试将失败,而另一个将成功。 however, both inheritance attempts fail in the above scenario. 但是,在上述情况下,两次继承尝试均失败。

这是因为字符串是internal字符串,所以仅限于自己的项目

Why don't they both work? 他们为什么都不起作用?

They should both fail if they contain the same code as you claim. 如果它们包含的代码与您声明的相同,则它们都将失败。 If that is not the case then the code is different between the 2 projects, specifically the MEMBER_1 is probably declared as public in InherTest project. 如果不是这种情况,那么这两个项目之间的代码不同的,特别是MEMBER_1可能在InherTest项目中声明为public

The only way that a reference to InherTest would work with the same code you posted is if you have this assembly level attribute InternalsVisibleToAttribute in the project InherTest InherTest的引用可以与您发布的相同代码一起工作的唯一方法是,如果您在项目InherTest具有此程序集级别属性InternalsVisibleToAttribute

[assembly:InternalsVisibleTo("InherTest.Base.Inherited")]

Inherited Namespaces are in different project. 继承的命名空间在不同的项目中。 (Name spaces seems like together but they are not in a same assembly). (名称空间看起来好像在一起,但它们不在同一程序集中)。 You can read that article. 您可以阅读该文章。

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/internal

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

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