我掙扎了一下,在這裏我將如何檢查瞭解迭代一塊,如果若跌破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 –