Permanent casting to a superclass

寵の児 提交于 2019-12-07 05:18:35

问题


If:

class Car : Automobile
{}

I can do:

Car toyota = new Car();
Automobile tauto = (Automobile)toyota;

but if I do tauto.GetType().Name it will still be Car.

Is it possible to perform a cast, so that the type is permanently changed to Automobile (without having to clone the object) ?

The problem i am trying to overcome is that there is no multiple inheritance in c#, and i need to merge objects (with the same signature) from 2 services, in one method, and return one type.


回答1:


No. There is no way to do this without constructing a new Automobile object.

However, there is also no reason to do this. The Liskov substitution principle says that any Car should, always, be treatable exactly like an Automobile, and the user should have no change in expected behavior.

As long as you design your class hierarchy correctly, using Car as an Automobile should always be perfectly acceptable.


On a side note: This is part of why using Type.GetType() is not the preferred way to check for type. It's much safer, and better, to use the is and as keywords in C#. They will return true if you check that tauto is Car.




回答2:


According to MSDN, you're just casting the reference to the object, and not the underlying object.




回答3:


Your question and what you're trying to do seem like two different things. No you can't change the underlying type of a object. But what you seem to want to do is create a type of automobile with set of properties and not actually use the subclass. What you could do is use a Automobile factory instead.

public static class AutomobileFactory()
{
   public static Automobile CreateAutomobile(AutomobileType type)
   {
     ...   
   } 
}

Where AutomobileType is a enum. For more information google C# Factory pattern.




回答4:


I'm not sure exactly what it is you're trying to do, but maybe you could consider a different approach? Instead of trying to merge the functionality of two different classes using inheritance, maybe you could use composition?

http://tiedyedfreaks.org/eric/CompositionVsInheritance.html

public class Service
{
    public virtual void DoSomething()
    {

    }
}

public class ServiceA : Service
{
    public override void DoSomething()
    {

    }
}

public class ServiceB : Service
{
    public override void DoSomething()
    {

    }
}

public class ServiceA_B : Service
{
    ServiceA serviceA = new ServiceA();
    ServiceB serviceB = new ServiceB();

    public override void DoSomething()
    {
        serviceA.DoSomething();
        serviceB.DoSomething();   
    }
}



回答5:


In some cases, generics may help. If a method includes a generic parameter, the generic type will be evaluated as the declared type of the passed-in reference, rather than the type of the passed-in object. For example:

void Foo(T bar)

If foo() is called with (Control)someButton, the type of 'bar' will be Button, but the type of T will be Control.



来源:https://stackoverflow.com/questions/4053636/permanent-casting-to-a-superclass

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