在C#中使用嵌套泛型类时发生继承问题 [英] Issue with Inheritance when using nested Generic classes in C#

查看:619
本文介绍了在C#中使用嵌套泛型类时发生继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个类层次结构,以便我可以拥有:

SpecificScreenController< ScreenController< Singleton



到目前为止,我已将这些设置为:

  public抽象类Singleton< T> :MonoBehaviour其中T:MonoBehaviour 
{
private static T _instance;
public static T实例{get {... return _instance;}}
}

public abstract class ScreenController< T> :Singleton< T> T:MonoBehaviour
{
public GAME_SCREEN GameScreen;
//更多ScreenController公共属性/字段/方法
}

公共类SpecificScreenController:ScreenController< SpecificScreenController>
{
//子类特定属性,覆盖ScreenController方法等
}

通过这种方式,我可以使用 SpecificScreenController.Instance.GameScreen; 到目前为止,这很好。



<例如:

  List< ScreenController> screenControllers = new List< ScreenController>(); 
screenControllers.Add(SpecificScreenController.Instance);
ScreenController s = screenControllers.Find(i => i.GameScreen == GAME_SCREEN.THING);

但是,当然...这不会编译,因为 ScreenController 现在需要一个泛型类型等等。我可以/应该使用什么成语来保留Singleton行为和ScreenController子类/超类?

解决方案

由于这个问题似乎没有真正干净的解决方案,我最终从我的类层次结构中删除了Singleton,并将单一属性的非泛型版本复制粘贴到实例化代码中,想成为单身人士。

  public abstract class ScreenController:MonoBehaviour 
{
}

public class SpecificScreenController:ScreenController
{
private static SpecificScreenController _instance;
public static SpecificScreenController实例{get {... return _instance;}
}


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

SpecificScreenController < ScreenController < Singleton

So far I have these set up as:

public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T _instance;
    public static T Instance{ get{... return _instance;} }
}

public abstract class ScreenController<T> : Singleton<T> where T : MonoBehaviour
{
    public GAME_SCREEN GameScreen;
    //many more ScreenController common properties/fields/methods
}

public class SpecificScreenController : ScreenController<SpecificScreenController> 
{
    //subclass specific properties, overriden ScreenController methods etc.
}

This way I can use SpecificScreenController.Instance.GameScreen; This works, so far, so good.

What I want to now do with this is, for instance:

List<ScreenController> screenControllers = new List<ScreenController>();
screenControllers.Add(SpecificScreenController.Instance);
ScreenController s = screenControllers.Find(i => i.GameScreen == GAME_SCREEN.THING);

But, of course ... this won't compile because ScreenController now requires a Generic Type etc. What idiom can/should I use to preserve the Singleton behavior and ScreenController sub/superclasses ?

解决方案

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;} 
}

这篇关于在C#中使用嵌套泛型类时发生继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆