我的建议是不要尝试使用继承。
任何结构都是可能的有效组件,例如:
struct Position
{
public int X, Y;
public Position(int x, int y)
{
X = x;
Y = y;
}
}
现在,您仍然可以Component<T>作为 int 字典 ( Dictionary<Type, int>) 的类型工作。虽然,我会做最少的改变来完成这项工作:
internal static class ComponentBit
{
public static int bitCounter = 0;
};
public static class Component<T>
{
public static readonly int bit; // This is static, it has a value per type T
static Component()
{
bit = ComponentBit.bitCounter++;
}
public static int GetBit()
{
// This method only accesses static members, it can be static
// Thus, no instance of T is needed
return bit;
}
}
你会像这样使用它:
Position pos = new Position(x, y);
int bit1 = Component<Position>.GetBit();
int bit2 = Component<Position>.bit;
不知道为什么你想要两种方法来做到这一点,但你去了。
好吧,现在我将自由地做更大的改变......考虑到这一点,如果你想要类型推断,你可以这样做:
public static class Component
{
internal static int bitCounter = 0;
public static int GetBit<T>()
where T : struct // We only accept structs here
{
return Component<T>.bit;
}
public static int GetBit<T>(this ref T value)
where T : struct // We only accept structs here
{
// This is an extension method.
// It will appear as a method on any valid T (which is all structs)
// The type T will be infered from the instance.
// Passing the struct as a reference to avoid a copy
_ = value; // discard value
return GetBit<T>();
}
};
internal static class Component<T>
where T : struct // We only accept structs here
{
internal static readonly int bit;
static Component()
{
bit = Component.bitCounter++;
}
}
用法:
var pos = new Position(x, y);
var bit1 = Component.GetBit<Position>();
var bit2 = pos.GetBit();
是的,上面的代码编译并运行。在SharpLab中查看。
注意:我会考虑嵌套Component<T>在Component.