I'm a little rusty with C# but something like this might work:
public List<Entity>List<FilterType> GetEntities<FilterType>()
{
// Assume all entities are valid
List<Entity> entities = new List<Entity>(allEntities);
var fields = FilterType.GetFields();
foreach(var field in fields)
{
var componentType = field.FieldType;
foreach(var entity in entities)
{
// Remove any invalid entities
if(!entity.HasComponent(componentType))
entities.Remove(entity);
}
}
List<FilterType> filters = new List<FilterType>();
foreach(var entity in entities)
{
FilterType filter = (FilterType) Activator.CreateInstance(FilterType);
foreach(var field in fields)
field.SetValue(filter, entity.GetComponent(field.FieldType));
filters.Add(obj);
}
return entities;filters;
}
This uses reflection to get the type of all (public!) fields in the struct/class FilterType and then checks if an entity does not have that component. In that case that entity is invalid and is removed from the final list result.