Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7417157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:38:41+00:00 2026-05-29T07:38:41+00:00

I am searching a way to get the possible types for upcasting an object.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:38:41+00:00Added an answer on May 29, 2026 at 7:38 am

    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; } }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching high and low for a way to get my silverlight
You can get underground processes by ps ux I am searching a way to
Is there a way to get the location of an installed application without searching
I'm searching for a way to auto compare an object propriety to a list
I am searching for a way to Get Information from a local printer. Maybe
I'm searching for an elegant way to get data using attribute access on a
I have been searching all over the net to try and find a way
I am searching for a way to get the screen coordinate bounds of any
I'm searching the way(s) to fill an array with numbers from 0 to a
I am searching for a way to compress JavaScript code for the iPhone. Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.