简体   繁体   English

无法在C#的派生类中访问基类属性

[英]cant access base class property in derived class in C#

Here is my base class 这是我的基础课

public class AttachmentBase
{
    public AttachmentBase ();

    public string[] Content { get; set; }
    public string FileExtension { get; set; }
    public string FileName { get; set; }
    public long Id { get; set; }
}

and here is my derived class 这是我的派生类

public class Attachment:AttachmentBase
{
    public string Title { get; set; }
    public string Description { get; set; }
}

and here is how I instantiate new attachment in mvc controller 这是我如何在mvc控制器中实例化新附件

var attachmnet = new Attachment();

but when i want to access the FileExtension or other property that are in base class I dont have access to them. 但是当我想访问基类中的FileExtension或其他属性时,我无权访问它们。 Everything is public but I do not have access to them. 一切都是公开的,但我无法访问它们。

I should say the base class in in another dll but I have added the reference to project. 我应该说基类在另一个dll中,但是我已经添加了对project的引用。

What is problem that i cant access base class property in derived class? 我无法在派生类中访问基类属性是什么问题?

If you simply create a base class and a derived class, there's no way you can have this issue. 如果仅创建基类和派生类,则无法解决此问题。 From what you said, it seems this happened because you created the base class in a project (= assembly = dll) and the derived one in another one, and instead of making this second project reference the first one, you made it reference a copy of a compiled dll. 从您所说的来看,似乎发生了这种情况,因为您在一个项目中创建了基类(= assembly = dll),而在另一个项目中派生了一个继承的类,而不是使第二个项目引用第一个项目,而是使其引用了一个副本。编译的dll。 This way of course you won't get the changes of the first project in realtime. 这样,您当然不会实时获得第一个项目的更改。

When a project references another one, Visual Studio always recompiles everything that's needed. 当一个项目引用另一个项目时,Visual Studio总是重新编译所需的所有内容。

Did Your check namespace? 您检查名称空间了吗? Because "AttachmentBase" class is under "System.Net.Main" namespace,and maybe you forget add correct project namespace that "AttachmentBase" is in it. 由于“ AttachmentBase”类位于“ System.Net.Main”命名空间下,因此您可能忘记添加其中包含“ AttachmentBase”的正确项目命名空间。

remove the reference to Attachment base dll in project , add it again , and rebuild solution . 在项目中删除对Attachment base dll的引用,再次添加它,然后重新生成解决方案。

i can see why c# cant be wise choose for productivity. 我明白了为什么不能明智地选择C#来提高生产率。

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

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