开发者

Casting one class to another which interited from same baseClass

It is possible to cast class1 to class2 ? result variable retrieves always null value.

class Program
{
    static void Main(string[] args)
    {
        Class1 class1 = new Class1();
        class1.Field1 = "1";
        class1.Field2 = "2";
        class1.Field3 = "3";
        class1.Field4 = "4";
        class1.Field5 = "5";
        BaseClass base1 开发者_如何学编程=  (BaseClass)class1;

        Class2 class2 = base1 as Class2;

        string result = class2.Field1;
    }
}

public class BaseClass
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Field3 { get; set; }
}

public class Class1 : BaseClass
{
    public string Field4 { get; set; }
    public string Field5 { get; set; }
}

public class Class2 : BaseClass
{
    public string Field6 { get; set; }
    public string Field7 { get; set; }
}


No, it's not possible to cast like this. You'll need to write your own conversion method or operator. The object base1 refers to simply isn't a Class2. What would its values of Field6 and Field7 be?


It's not possible to cast Class1 to Class2. But to BaseClass is, of course, possible.


"Can it be done?"

No, they are both BaseClass and can both be cast to BaseClass, but you cannot cast Class1 to Class2 or vice versa. Nor can BaseClass be cast to Class1. You can only cast upwards in the inheritance chain.

If you are trying to overcome cumbersome mappings or something, consider AutoMapper for your mappings which is really helpful in those cases.


The problem here is that your Class1 cannot be casted as Class2.

When you do

BaseClass base1 =  (BaseClass)class1;

You are only changing the reference and not the object itself. Object knows that it is a Class1. So when you try to cast as Class2, it fails since one is not inherited from the other.


The problem line is:

Class2 class2 = base1 as Class2;

The problem is that this would have no idea what to do with Field6 or Field7. You have to write your own conversion so that these fields get sorted out.


Assuming that the question is "Why does string result = class2.Field1; die with a NullReferenceException" I'll try to explain.

Lets rename the classes a bit. Lets say BaseClass is Vehicle, Class1 is Car, and Class2 is SpaceShuttle.

So,

BaseClass base1 =  (BaseClass)class1; 
//becomes
Vehicle myVehicle =  (Vehicle) myCar; 

No problem there, the cast is not even neccessary. A car is always a vehicle. But

Class2 class2 = base1 as Class2;
//becomes
SpaceShuttle shuttle = myVehicle as SpaceShuttle.

Here i'm saying that my vehicle is a spaceShuttle, when in fact is an old Honda Civic. The compiler notices this, and sets the shuttle variable to null, because thats what the as operator does. If you ask myVehicle is it a SpaceShuttle, it will respond with a NO, like this:

bool isShuttle = (myVehicle is SpaceShuttle); //false

So even if both SpaceShuttle and Car have the same property, inherited from Vehicles, lets say Color or Owner or whatever, it does not mean that I can treat my car as a space shuttle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜