Blackshark
Blackshark

Reputation: 45

Can I extend a base class and cast the base to the extension

I have write this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Mail;
using System.Net;

public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Foo m_oFoo = new Foo() { 
            S1 = "N"
        };

        List<Foo> m_List = new List<Foo>();
        m_List.Add(m_oFoo);

        IFoo m_oIFoo = (IFoo)m_List[0];
        m_oIFoo.S2 = "C";

        Response.Write(m_oIFoo.S1);
        Response.Write(m_oIFoo.S2);
    }
}

public class Foo
{
    public string S1 { get; set; }
    public Foo()
    {

    }
}

public class IFoo: Foo
{
    public string S2 { get; set; }
    public IFoo(){}
}

But the compiler say's "Unable to cast object of type 'Foo' on type 'IFoo'." How I can Cast Foo to Foo1 without declare a caster with all Foo methods because this is an example, Foo has more than 100 methods.

thanks for the help

Upvotes: 0

Views: 2540

Answers (6)

Srikanth Venugopalan
Srikanth Venugopalan

Reputation: 9049

You cannot cast from base class to derived class. Instead, try writing an explicit converter and use AutoMapper to map all the properties.

Upvotes: 0

Toni Petrina
Toni Petrina

Reputation: 7122

First of all, please refrain from having capital first I in your classes, that is usually used for interfaces. Second of all, you cannot cast "upwards" because IFoo inherits Foo, not the other way around.

Think about it in this way: IFoo is a Foo, but not the other way round.

You can, however, rewrite it to this:

IFoo m_oIFoo = m_List[0] as IFoo; // this might be null now
if (m_oIFoo != null)
{
    m_oIFoo.S2 = "C";

    Response.Write(m_oIFoo.S1);
    Response.Write(m_oIFoo.S2);
}

Use the as operator for casting, but be aware that it can return null.

Upvotes: -1

Servy
Servy

Reputation: 203840

How I can Cast Foo to Foo1

You can't. Ever. The object really is a Foo object, not an IFoo, so no cast will ever succeed. You need to "convert" the object, which is a very different thing. You need to create a new IFoo object based on the data provided in a Foo instance. One way of doing this is though a constructor in IFoo that accepts a Foo object:

public class IFoo: Foo
{
    public string S2 { get; set; }
    public IFoo(){}
    public IFoo(Foo other)
    {
        S1 = other.S1;
    }
}

Then you can do:

IFoo m_oIFoo = new IFoo(m_List[0]);

Upvotes: 2

p.s.w.g
p.s.w.g

Reputation: 149050

You cannot cast a base class to a subclass, but you can do something similar with a conversion operator.

public class IFoo: Foo
{
    ...

    public static explicit operator IFoo(Foo foo)  // explicit byte to digit conversion operator
    {
        ...
    }
}

Upvotes: 0

Joe Enos
Joe Enos

Reputation: 40413

Sorry, no built-in way of doing it. Best bet is to use either reflection to retrieve all the properties that exist in both classes and set the values that way, or possibly serialize the object and then deserialize it into the other type.

Neither way is perfect.

Upvotes: 0

Mike Dinescu
Mike Dinescu

Reputation: 55750

You can't cast a base class into one of it's derived (extended) classes. This is pretty intuitive if you think about the process of extending a class in general. When extending a class, the extended class might add members which will not be present in the base class so when if the compiler allowed the cast then potentially disastrous things could happen!

Upvotes: 0

Related Questions