尝试比较通用枚举时出现编译器错误。一切看起来很好,我不知道什么是错的。
错误讯息:Operator == cannot be applied to operands of type T and T
public class SpriteLink<T>
{
public T id;
public Sprite sprite;
public SpriteLink(T id, Sprite sprite)
{
this.id = id;
this.sprite = sprite;
}
}
public class LinkList<T>
{
public List<SpriteLink<T>> links = new List<SpriteLink<T>>();
public bool Contains(T id)
{
links[0].id = id; // compiler passes this line without any errors
return links.Any(link => link.id == id); // but throws compiler error here (why?)
}
public bool Set(T id, Sprite sprite)
{
if (!Contains(id))
{
links.Add(new SpriteLink<T>(id, sprite));
return true;
}
return false;
}
}
public LinkList<SurfaceID> surfaceLinks = new LinkList<SurfaceID>();
public LinkList<FloorID> floorLinks = new LinkList<FloorID>();
SurfaceID
和FloorID
只是包含某些字段的枚举。