Cast parent class to child class

家住魔仙堡 提交于 2019-12-23 09:48:12

问题


I have Message class which I have extended and added new property.

class ChildMessage: Message 
{
prop...
}

While trying to add Message Class to ChildMessage list I get Null reference for added class.

var myChildList =new List<ChildMessage> ();
var myParentClass = new Message();
myChildList.add(myParentClass as ChildMessage)

myChildList[0] == null //Always return null 

What is wrong with my code? What are the alternatives?


回答1:


It is because a true instance of Message is not a ChildMessage, whereas an instance of ChildMessage is a Message.

Unless myParentClass is actually instantiated with ChildMessage you cannot use the cast operator to force a forward cast as the actual underlying type is Message.

That said, there are implicit and explicit cast operators available for use on types, so you could perhaps create one that takes a Message and outputs a ChildMessage.

http://msdn.microsoft.com/en-us/library/85w54y0a.aspx


Knowing that you are given these types and you have no choice but to use them, you have two options in order to extend that type.

First Option

Use the decorator/wrapper pattern to wrap a Message inside a ChildMessage:

class ChildMessage
{
    private Message _innerMessage;

    public ChildMessage(Message innerMessage)
    {
        _innerMessage = innerMessage;
    }
}

You then expose either the Message or its properties through the ChildMessage class.

Second Option

This is just as feasible, inherit from the Message, but when you want to make a ChildMessage from a Message you will need to convert it, not cast it:

class ChildMessage : Message
{
    public ChildMessage(Message m)
    {
        // Instantiate properties by copying from m
    }
}



回答2:


I think you somehow got it the wrong way - normaly you might want something like:

var myList =new List<Message> ();
var myChildObject = new ChildMessage();
myList.add(myChildObject)

myList[0] as ChildMessage == null // should not return null



回答3:


If you change your code in this way

var myChildList =new List<Message> (); 
var myChildClass = new ChildMessage(); 
myChildList.add(myChildClass as Message) 

Now you have a List where the objects are of type ChildMessage or other types derived from Message. You could check the correct type later when you use the objects stored in the list.




回答4:


A cast on an object cannot be used to change its type: it is not a conversion. It only serves to tell the type of an object: you explain that you want to use the object as an object of that type; and the object must already be of that type, or you'll get an error (at least in a decent language such as C#).

Casting objects is meaningful because, thanks to inheritance, objects may be of multiple types at once.

In your code, every ChildMessage is a Message, but the Messages you create are not ChildMessages, so trying to use them as such fails.

By contrast, "casts" from, say, integers to reals, or vice versa, may actually be conversions. (Terminology in IT is a hopeless jungle.)



来源:https://stackoverflow.com/questions/9598381/cast-parent-class-to-child-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!