我挣扎了一下,在这里我将如何检查了解迭代一块,如果若跌破entity
变量在变量comps
所有Enum.Component
项。如果我知道只有一个组件通过.ForEach
和基本比较(例如entity.ForEach(comp => Console.WriteLine(comp.COMPONENT == Enum.Component.EXPERIENCE));
),我可以比较直接地实现,但如果需要检查多个组件,则不会。检查实体包含所有组件通过枚举标志
我想了解的C#好一点细微的差别,所以我不想蛮力这与实际foreach
(在常规foreach(var x in exes)
类型的方式)或类似的东西,但真正要了解我如何通过这些IEnumerable
函数并使用lambda表达式来实现这些对象。因此,我需要一个利用这些东西的答案,除非这在技术上是不可行的,尽管可能是这样,我猜测。
// The Component.IComponent Interface (it's in the Component namespace)
interface IComponent {
Enum.Component COMPONENT {
get;
}
}
// The Enum.Component (it's in the Enum namespace)
enum Component {
EXPERIENCE,
HEALTH
}
// The Component.Experience (it's in the Component namespace)
class Experience : IComponent {
public ThresholdValue xp;
public int level;
public Enum.Component COMPONENT {
get {
return Enum.Component.EXPERIENCE;
}
}
}
// It probably doesn't matter, but ENTITY_MANAGER is this type
Dictionary<Guid, List<Component.IComponent>>
// Trial code beings here:
Guid GUID = new Guid();
ENTITY_MANAGER.getEntities().Add(GUID, new List<Component.IComponent> { new Component.Experience(50, 3), new Component.Health(20, 25) });
List<Component.IComponent> entity = ENTITY_MANAGER.getEntities()[new Guid()];
Enum.Component[] comps = new Enum.Component[] {
Enum.Component.EXPERIENCE,
Enum.Component.HEALTH
};
// This is where I don't know what to do and know this is wrong
comps.All(v => entity.ForEach(comp => Console.WriteLine(comp.COMPONENT == v)));
你可以在实体中使用select! https://msdn.microsoft.com/en-us/library/jj573936(v=vs.113).aspx –