简体   繁体   中英

Generic Type conversion error on inherited classes

im trying to create a an mvc framework to define the workflow of a project im working on. I created the view and controller classes and was trying to have the ui controller class be generic in order to have a type of uiview assigned to it when inheriting from ui controller:

UIView class

    public class UIView
    {
    }

UIController class

    public class UIController<T> where T : UIView
    {
    }

Implementation of UIView class

    public class UIMenuView : UIView
    {
    }

Implementation of UIController class with UI Menu View

    public class MenuController : UIController<UIMenuView>
    {
    }

Workflow implementation

    void main()
    {
        var menuController = new MenuController();
        SetController(menuController as UIController<UIView>);
    }

    public void SetController(UIController<UIView> controller, bool asRoot = false)
    {
    }

The problem is im getting the following error while trying to set the controller:

Cannot convert type 'MenuController' to 'UI.UIController<UI.UIView>' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion [Assembly-CSharp]

Does anybody know what im doing wrong here? Thanks!

Following picture shows your inheritance hierarchy. Your casting menuController as UIController<UIView> is what described with red arrow which is wrong.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM