Issue with Inheritance when using nested Generic classes in C#

前端 未结 2 631
旧巷少年郎
旧巷少年郎 2020-12-22 06:11

I\'m trying to create a class hierarchy such that I can have:

SpecificScreenController < ScreenController < Singleton

So far I have these set up as:

相关标签:
2条回答
  • 2020-12-22 06:11

    The problem here is one of covariance. You're assuming that if SpecificScreenController inherits from MonoBehaviour then ScreenController<SpecificScreenController> also inherits from ScreenController<MonoBehaviour>. It doesn't. You can't do this cast.

    0 讨论(0)
  • 2020-12-22 06:21

    As there seem to be no really clean solutions to this issue I ended up removing Singleton from my class hierarchy and copy-pasted non-generic versions of the singleton property get/instantiate code into each of the classes that I wanted to be singletons.

    public abstract class ScreenController : MonoBehaviour
    {
    }
    
    public class SpecificScreenController : ScreenController
    {
        private static SpecificScreenController _instance;
        public static SpecificScreenController Instance{ get{... return _instance;} 
    }
    
    0 讨论(0)
提交回复
热议问题