Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upcast an object

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.

like image 574
BennoDual Avatar asked Feb 09 '26 23:02

BennoDual


1 Answers

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)

like image 146
Bruno Costa Avatar answered Feb 12 '26 16:02

Bruno Costa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!