简体   繁体   English

C#和继承。当对象是扩展类对象时,基类属性为null

[英]C# and inheritance. Base class property is null when the object is Extended class object

I have the following classes: 我有以下课程:

public class BaseContainer
{
   public BaseItem item {get; set;}
}

public class ExtendedContainer : BaseContainer
{
   new public ExtendedItem item {get; set;}
}

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

public class ExtendedItem : BaseItem
{
   public string Surname { get; set; }
}

Then I have the following instructions: 然后我有以下说明:

ExtendedItem ei = new ExtendedItem { Name = "MyName", Surname = "MySurname" };
ExtendedContainer ec = new ExtendedContainer { item = ei };
BaseContainer bc = ec;
string temp = bc.item.Name;  // bc.item is null, why?

Why bc.item is null? 为什么bc.item为空? It shouldn´t because ExtendedItem is a subclass if BaseItem, but when I run the code it is null. 它不应该因为ExtendedItem是BaseItem的子类,但是当我运行代码时它是null。 Can anyone help me with this issue? 任何人都可以帮我解决这个问题吗?

The reason I´m doing this is that I´m using MVC3 and I´m passing to partial views objects/models that are part of a similar structure, and I normally just need the properties in the base class. 我这样做的原因是我使用MVC3并且我传递给部分视图对象/模型是类似结构的一部分,我通常只需要基类中的属性。

Thanks in advance. 提前致谢。

UPDATE UPDATE

Jon is right, I´m accessing different objects. Jon是对的,我正在访问不同的对象。 What I did to fix it is to use constructors to set the item properties to the same object. 我做的是修复它是使用构造函数将项属性设置为同一个对象。 Like this: 像这样:

public class BaseContainer
{
    public BaseItem item {get; set;}
    public BaseContainer(BaseItem item)
    {
        this.item = item;
    }
}

public class ExtendedContainer : BaseContainer
{
    public ExtendedItem item {get; set;}

    public ExtendedContainer(ExtendedItem item) : base(item)
    {
        this.item = item;
    }
}

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

public class ExtendedItem : BaseItem
{
    public string Surname { get; set; }
}

And then I just create the object: 然后我只是创建对象:

    ExtendedItem ei = new ExtendedItem { Name = "MyName", Surname = "MySurname" };
    ExtendedContainer ec = new ExtendedContainer(ei);
    BaseContainer bc = ec;
    string temp = bc.item.Name;

it works now 它现在有效

You've used the new keyword, which hides the original base property in BaseContainer. 您已经使用了new关键字,它隐藏了BaseContainer中的原始基本属性。

When you instantiate ExtendedContainer, you're setting the new property on the child class rather than the original property on the base class. 实例化ExtendedContainer时,您要在子类上设置新属性,而不是在基类上设置原始属性。

Just remove the property from the child class and everything should work. 只需从子类中删除该属性,一切都应该工作。

You've got two entirely separate properties here, which can store different values. 这里有两个完全独立的属性,可以存储不同的值。

You've set the ExtendedContainer.item property on the object, but you haven't set the BaseContainer.item property. 您已在对象上设置ExtendedContainer.item属性,但尚未设置BaseContainer.item属性。

Imagine the two properties had entirely different names - you wouldn't expect setting one to change the other then, would you? 想象一下,这两个属性有完全不同的名称 - 你不会指望设置一个改变另一个,那么,你呢? Well as far as the CLR is concerned, the fact that the two properties have the name is merely a coincidence. 就CLR而言,这两个属性具有名称的事实仅仅是巧合。

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

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