Extended class attributes/methods referenced as base class attributes/methods

I have a few classes that extend MonoBehaviour, for example; WidgetController and PanelController.

From these classes I extend various other classes; ButtonController : WidgetController and PanelLoginController : PanelController.

The definition of the WidgetController has a public PanelController attribute

public class WidgetController : MonoBehaviour {
public PanelController panelController;

}

Inherently all of the different types of widget controllers (buttons, checkboxes etc) have a PanelController panelControoler attribute. In the editor they are referenced to extended classes of PanelControllers, such as a PanelLoginController.

Any attributes or methods in PanelController are avaiable in the extended WidgetController, as expected. However, when I create a specialized method or attribute in a extented PanelContrller, the PanelController panelController attribute does not reflect these.

Example:
public class PanelLoginController : PanelController {

public UIInput inputUsername;
public UIInput inputPassword;

private void AttemptLogin() {
    string username = inputUsername.label.text;	
    string password = inputPassword.label.text;
}

}

public class PanelLoginButtonLogin : ButtonController {

void OnClick() {
    Debug.Log(panelController.GetType()); // returns PanelLoginController
    panelController.AttemptLogin(); // error
    panelController.inputUsername = ""; // error
    
    panelController.Invoke("AttemptLogin", 0f); // this works, but I cannot pass parameters and it seems like a work around 
}

}

I am trying to wrap my head around Generics, but havn’t quite figured them out yet. I believe they may be the required course of action, but I am not entirely sure.

One work around would be to define a new attribute of the extented panel controller in each extented widget controller- on construct cast the PanelController panelController to that attribute. This does not seem like the proper approach, and would require more code maintenance.

Any suggestions or solutions are greatly appreciated.

Cheers!

I’d do something like this:

   void OnClick() {

         var loginController = panelController as PanelLoginController;
         if(loginController == null) 
               throw new Exception("Panel controller wasn't a login controller");

         loginController.AttemptLogin();
         loginController.inputUserName = "";
            
   }

You could define a generic base class:

   public class WidgetController<T> : WidgetController where T : PanelController
   {
            public new T panelController
            {
                get
                {    
                     return base.panelController as T;
                }
                set 
                {
                     base.panelController = value;
                }
            }
   }

Then you could just do:

        public class PanelLoginButtonLogin : WidgetController<PanelLoginController>
        {
                void OnClick()
                {
                      panelController.AttemptLogin();
                      panelController.inputUserName = "":
                }
        }