我不确定标题是否反射(reflect)了我的意思,但是..
假设我有两个类(class),Entity
和 Component
:
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<T>
继承 Component
:
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; }
}
}
现在,假设我有 Foo
, Bar
和 Processor
类:
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!
有什么想法吗?
最佳答案
您的帖子不清楚您对基本 Entity
有什么限制(如果有的话)和 Component
类。所以我不知道以下内容在您的场景中是否可行。也就是说,我相信如果不是这样,您将无法执行您想要的操作,否则编译器将不知道泛型类型参数。
在没有任何其他限制的情况下,解决方案是使您的 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<T>
调用它对象,你会得到一个编译器错误:
Foo foo = new Foo();
Bar bar = new Bar();
P p = new P();
foo.AddComponent(p);
bar.AddComponent(p); // CS0311
注意:我强烈建议不要隐藏类(class)成员。它并没有真正影响您所述的问题(即您可以完全忽略该细节),但是拥有两个具有相同名称的不同属性只是在寻找错误。如果您必须使用隐藏,恕我直言,您至少应该让新属性使用隐藏属性。例如:
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
,至少你会得到一个运行时错误属性作为通用版本,如果以及何时由于某种原因由基类型分配了错误的类型。
关于c# - 从另一个泛型类添加泛型方法约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35970523/