3

I am searching a way to get the possible types for upcasting an object. For example: I have a control of type MyControl which inherits Control. Now, when the object of type MyControl is downcasted to Control is there a way to find out, if it is the top object-type or when now to get the type(s) in which it can be upcasted (in this case MyControl)? I want it upcast to MyControl (with Reflection) and get a Property with reflection. But I don't know MyControl at the place where I have to do this.

MyControl is implement Visible with new. Now when I call control.Visible = true it will call the Visible of Control but I have to call the Visible of MyControl.

Thanks for your help.

soniiic
  • 2,664
  • 2
  • 26
  • 40
BennoDual
  • 5,865
  • 15
  • 67
  • 153
  • 2
    I believe you have reversed the meaning of upcasting and downcasting. Upcasting is when you convert from a derived class to a base class **up** the inheritance hierarchy (e.g. `MyControl` to `Control`). Downcasting is the opposite way where you cast **down** the inheritance hierarchy. In C# upcasting is implicit and something you don't have to write out in code. – Martin Liversage Feb 07 '12 at 15:08

4 Answers4

4

You can also use this:

MyControl myControl = someControlOfTypeMyControl as MyControl

if(myControl != null)
{
   //your stuff
}

With "as" .net framework checks if the control is from that type and if it is possible to cast the .NET Framework will cast and return with type MyControl, otherwise it will return null.

So basically, its the same as previous answers, but more clean (imho, you can think different)

Bruno Costa
  • 2,708
  • 2
  • 17
  • 25
2

There is:

if (myControl is MyControl)
{
    var m = (MyControl)myControl;
}

This will work on any part of the type hierarchy. The following check will not work if the variable itself is of a base type:

MyBaseControl myControl = null;

if (myControl.GetType() == typeof(MyControl))
{

}

However it sounds like you want the behaviour of overridden methods or properties. In the normal situation, you would override Visible:

public override bool Visible
{
    get { return true; } // Always visible in derived class.
}

However this is only if the base class is not sealed and the member you want to override is abstract or virtual. If this is not the case, then I'd stick with casting to the derived type... not ideal, but not many options.

It also sounds like you tried to hide the base member like this:

public new bool Visible
{
    get { return true; }
}

This only works if you have a reference to the type itself. If you have a reference to the base type, the member hiding does not work, it doesn't know the member is hidden in the derived type:

MyBaseControl c = new MyDerivedControl();

bool vis = c.Visible; // Comes from MyBaseControl even if hidden in derived control.

(In the above, if Visible were overridden, then it would come from the derived class).

Update: to do any of this at runtime, you can do the following so long as you know the names of the things you want to reflect:

class Program
    {
        static void Main(string[] args)
        {
            A a = new B();

            // Get the casted object.
            string fullName = a.GetType().FullName;
            object castedObject = Convert.ChangeType(a, Type.GetType(fullName));

            // Use reflection to get the type.
            var pi = castedObject.GetType().GetProperty("Visible");

            Console.WriteLine(a.Visible);
            Console.WriteLine((bool)pi.GetValue(castedObject, null));

            Console.Read();
        }
    }    

    class A
    {
        public bool Visible { get { return false; } }
    }

    class B : A
    {
        public new bool Visible { get { return true; } }
    }
}
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
0
public class Animal

{

    public void Speak()
    {
        Console.WriteLine("Animal is Speak");
    }

    public void Move()
    {
        Console.WriteLine("Animal is Move");
    }
    public void Eat()
    {
        Console.WriteLine("Animal is Eat");
    }
}
public class HumingBird:Animal

{

    public void Fly()
    {
        Console.WriteLine("Huming Bird is Fly");
    }
}


class Program

{
    static void Main(string[] args)
    {
    
        Animal animal = new HumingBird();

     
        HumingBird humingBird = (HumingBird)animal;
        humingBird.Fly();


    }
}

Upcasting is placing an object in a child class with a reference to the Base class

Animal animal = new HumingBird(); // Upcasting

Down casting is Explicit casting by placing a reference to the base class in the reference of the child class

HumingBird humingBird = (HumingBird)animal;

humingBird.Fly();
cigien
  • 57,834
  • 11
  • 73
  • 112
MH Abir
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 15 '23 at 00:23
0
Control control = (control)someControlOfTypeMyControl;

if (control is MyControl) {
    var myControl = (MyControl)control;
    var propertyValue = myControl.SomeProperty;
}
soniiic
  • 2,664
  • 2
  • 26
  • 40