Share via


C# Cast derived class type to this of parent class using Type

Question

Monday, July 8, 2019 6:36 PM

Hello all!

I'm trying to use bit of my code like this:

class Parent
{
public Parent (Type child)
{
ChildA_or_B_Type foo = (child)this; // an error occured here
}
}

class ChildA : Parent
{
public ChildA () : base (this.GetType())
{

}
}

class ChildB : Parent
{
public ChildB () : base (this.GetType())
{

}
}

How to implement this convertation properly? I haven't found any solution.

Thank you.

All replies (5)

Monday, July 8, 2019 7:40 PM

So, what is actually the type of the variable foo? You declared it as ChildA_or_B_Type, which you have not defined anywhere. If you want to put anything that is either A or B into that variable, then the variable would have to be of a class that is the parent of both A and B. You already have one such class, which happens to be, precisely, Parent. In which case you wouldn´t need any cast at all. So the line that produces the error would be written like this:

Parent foo = this;


Tuesday, July 9, 2019 4:44 AM

For example, I need to do each step of this ChildA type variable convert: ChildA -> Type -> ChildA

where I could replace ChildA with any reference type. Now, I have an error on compilation: The type or namespace name 'child' could not be found (are you missing a using directive or an assembly reference?)

Can I do this?


Tuesday, July 9, 2019 5:21 AM

For example, I need to do each step of this ChildA type variable convert: ChildA -> Type -> ChildA

You can only do this if "Type" is a parent type of ChildA.

where I could replace ChildA with any reference type

Then "Type" has to be Object. Otherwise it won't be the parent of any reference type.

So you need to do ChildA -> Object -> ChildA.

No cast is needed for this:

Object foo = this;

And yes, I know that you wanted to do (child)this. No, you can't do that (the cast needs to be known at compile time, it cannot be a variable) and in fact it is unneeded, because the object will "know" internally the type of child that you assigned and remember it. So you will be able to later convert it back to Child:

if (foo is ChildA) { ChildA bar = (ChildA)foo; }


Tuesday, July 9, 2019 5:22 AM

Greetings Desert Winter.

I can't understand what you are trying to achieve. Why would the base class (Parent) need a reference to itself cast to the class derived from it (ChildA)?

I strongly suspect you are going about things the wrong way. Can you show us a (preferably simple) example of what you are trying to do and why you think you need to perform that cast? I think if we can understand your ultimate goal (what you want to do if you get the cast working) we might be able to offer better advice.


Tuesday, July 9, 2019 6:01 AM

This seems to compile:

class Parent

{

    public Parent( )

    {

        switch( this )

        {

        case ChildA a:

            // . . .

            break;

        case ChildB b:

            // . . .

            break;

        }

 

    }

}

 

class ChildA : Parent

{

    public ChildA( )

    {

 

    }

}

 

class ChildB : Parent

{

    public ChildB( )

    {

 

    }

}

 

However probably there are more appropriate solutions (e.g. virtual functions) for your specific problems.