【问题标题】:Adding a generic method constraint from the another generic class从另一个泛型类添加泛型方法约束
【发布时间】:2016-06-28 11:41:15
【问题描述】:

我不确定标题是否反映了我的意思,但是..
假设我有两个班级,EntityComponent

public abstract class Entity
{
    private List<Component> _components = new List<Component>();

    public void AddComponent<T>()
        where T : Component
    {
        T component = (T)Activator.CreateInstance(typeof(T));
        component.Owner = this;

        _components.Add(component);
    }
}

public abstract class Component
{
    public Entity Owner { get; protected set; }

    public abstract void Update();
}

您可能会注意到,上面的类是abstract classes,这意味着不适合直接使用。但是,在开发的后期,我知道某些Component 需要仅可附加/由继承到Entity 类的特定类添加的能力。

所以,我添加了一个继承 Component 的类 Component&lt;T&gt;

public abstract class Entity
{
    private List<Component> _components = new List<Component>();

    public void AddComponent<T>()
        where T : Component
    {
        T component = (T)Activator.CreateInstance(typeof(T));
        component.Owner = this;

        _components.Add(component);
    }
}

public abstract class Component
{
    public Entity Owner { get; protected set; }

    public abstract void Update();
}

public abstract class Component<T> : Component
{
    // I hide the base.Owner with new keyword
    // feel free to suggest me in case there is better approach to do this
    new public T Owner 
    { 
        get { return (T)base.Owner; } 
        protected set { base.Owner = value; }
    }
}

现在,假设我有 FooBarProcessor 类:

public class Foo : Entity
{
    public int FooValue { get; set; }
}

public class Bar : Entity
{
    public int BarValue { get; set; }
}

public class Processor : Component<Foo>
{
    public override void Update()
    {
        Owner.FooValue = 10;
    }
}

我想做的是使Processor 类只能由Foo 对象添加。目前AddComponent忽略它,所以我不知道该怎么做:

var foo = new Foo();
var bar = new Bar();

foo.AddComponent<Processor>(); // OK
bar.AddComponent<Processor>(); // Compiler should give an error at this point

我也尝试过这样做:

public void AddComponent<T, X>()
    where T : Component<X>
    where X : Entity
{
    T component = (T)Activator.CreateInstance(typeof(T));
    component.Owner = this;

    _components.Add(component);
}

但是,它需要我明确指定 X 约束:

foo.AddComponent<Processor, Foo>();
bar.AddComponent<Processor, Bar>(); // Error, but the syntax is weird!

有什么想法吗?

【问题讨论】:

    标签: c# .net generics


    【解决方案1】:

    您的帖子不清楚您的基本 EntityComponent 课程有哪些限制(如果有)。所以我不知道以下在您的情况下是否可行。也就是说,我相信如果不是,您将无法做您想做的事情,因为否则编译器将不知道泛型类型参数。

    在没有任何其他约束的情况下,解决方案是使您的 Entity 类通用,并提供子类类型本身作为类型参数:

    class Entity { }
    
    class Entity<T> : Entity where T : Entity<T>
    {
        public void AddComponent<U>(U value) where U : Component<T> { }
    }
    
    class Component<T> where T : Entity { }
    
    class Foo : Entity<Foo> { }
    
    class Bar : Entity<Bar> { }
    
    class P : Component<Foo> { }
    

    我知道这看起来很奇怪。但是你基本上是在要求一个泛型类型依赖的自引用图,而在 C# 代码中,上面就是这样的。

    您可以使用类型推断调用AddComponent() 方法(因此不需要泛型参数)。如果您尝试使用错误类型的 Component&lt;T&gt; 对象调用它,您将收到编译器错误:

    Foo foo = new Foo();
    Bar bar = new Bar();
    P p = new P();
    
    foo.AddComponent(p);
    bar.AddComponent(p); // CS0311
    


    注意:我强烈建议不要隐藏班级成员。它并不会真正影响您所说的问题(即您可以完全忽略该细节),但是拥有两个具有相同名称的不同属性只是在询问错误。如果您必须使用隐藏,恕我直言,您至少应该让新属性使用隐藏属性。例如:

    class Component
    {
        public Entity Owner { get; protected set; }
    }
    
    class Component<T> : Component where T : Entity
    {
        new public T Owner
        {
            get { return (T)base.Owner; }
            set { base.Owner = value; }
        }
    }
    

    您不会在编译时检查对非泛型 Component.Owner 属性的分配,但如果某些代码尝试将 Owner 属性取消引用为泛型,至少您会收到运行时错误版本,如果以及何时由于某种原因基类型分配了错误的类型。

    【讨论】:

    • 感谢您的回答,这似乎符合我想要的东西!但是,在某些情况下FooBar 被另一个类继承,考虑这两个类:A : FooB : Bar(当然还有Foo : Entity&lt;Foo&gt;Bar : Entity&lt;Bar&gt;),在这种情况下,是不是可以创建一个只能由类 B 附加的 Component
    • 取决于您的确切意思。可以使基类泛型(class Foo&lt;T&gt; : Entity&lt;T&gt; where T : Entity&lt;T&gt;),然后可以在派生类中指定类型参数:class A : Foo&lt;A&gt;class B : Foo&lt;B&gt;。您甚至可以制作Foo 的非通用版本:class Foo : Foo&lt;Foo&gt;。以上内容应解决您最初陈述的问题。如果您想探索其他替代方案,我建议您尝试上述方法;如果你遇到麻烦,你可以随时发布一个新问题来引用这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多