views:

120

answers:

5

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.

+9  A: 

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.

Reed Copsey
unfortunately wcf serializer cares, and 3.5 generics also cares.. so i can't simply change that code.. i know i could provide my own serialization but it gives me lots of headaches all over the place..
Sonic Soul
3.5 generics don't care - granted, there are some potential issues with WCF serialization, though that's rare, and you should be making your class serializable anyways. If you really need to make it a subclass, you'll need to construct a new object. Just make a new constructor on Automobile that's like this: `public Automobile(Automobile other)`, and then you can create that when passing to a routine that really cares...
Reed Copsey
A: 

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

Matthew Graybosch
A: 

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.

Will
A: 

Hi. 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();   
    }
}
Brandon
A: 

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.

supercat