如何确保C#中的列表始终具有特定元素?
我有一个名为GameObject的类,它有一个组件的动态列表。可以随时删除和添加这些组件。组件可能是纹理、声音或过渡等。确保此列表始终有一个过渡组件的最佳方法是什么?每种类型只允许一个组件 我脑子里有两个可能的解决办法。哪一个是最好的?有没有更好的办法来解决这个问题 方法1:如何确保C#中的列表始终具有特定元素?,c#,list,elements,C#,List,Elements,我有一个名为GameObject的类,它有一个组件的动态列表。可以随时删除和添加这些组件。组件可能是纹理、声音或过渡等。确保此列表始终有一个过渡组件的最佳方法是什么?每种类型只允许一个组件 我脑子里有两个可能的解决办法。哪一个是最好的?有没有更好的办法来解决这个问题 方法1: class GameObject { private List<Component> components; public T GetComponent<T>() where T
class GameObject {
private List<Component> components;
public T GetComponent<T>() where T : Component {
// search the requested component and return it
foreach(Component comp in components) {
if(comp is T) return (T)comp;
}
// return null or throw exception when not found
return null;
}
public void RemoveComponent<T>() where T : Component {
if(typeof(T) != typeof(Transition)) {
// only remove the componenent if it's not a Transition component
Component tmp;
foreach(Component comp in components) {
if(comp is T) tmp = comp;
break;
}
components.Remove(tmp);
}
}
}
class GameObject {
// separate variable for the transition component
private Transition transition;
private List<Component> components;
public T GetComponent<T>() where T : Component {
// if a transition is requestet just return it
if(typeof(T) == typeof(Transition)) {
return transition;
}
// else: search the requested component in the list
foreach(Component comp in components) {
if(comp is T) return (T)comp;
}
// return null or throw exception when not found
return null;
}
public void RemoveComponent<T>() where T : Component {
if(typeof(T) != typeof(Transition)) {
// only remove the componenent if it's not a Transition component
Component tmp;
foreach(Component comp in components) {
if(comp is T) tmp = comp;
break;
}
components.Remove(tmp);
}
}
}
类游戏对象{
私有列表组件;
public T GetComponent(),其中T:Component{
//搜索请求的组件并返回它
foreach(组件中的组件comp){
如果(comp为T)返回(T)comp;
}
//未找到时返回null或引发异常
返回null;
}
public void RemoveComponent(),其中T:Component{
if(类型(T)!=类型(转换)){
//仅当组件不是转换组件时才移除该组件
成分tmp;
foreach(组件中的组件comp){
如果(comp为T),则tmp=comp;
打破
}
组件。移除(tmp);
}
}
}
方法2:
class GameObject {
private List<Component> components;
public T GetComponent<T>() where T : Component {
// search the requested component and return it
foreach(Component comp in components) {
if(comp is T) return (T)comp;
}
// return null or throw exception when not found
return null;
}
public void RemoveComponent<T>() where T : Component {
if(typeof(T) != typeof(Transition)) {
// only remove the componenent if it's not a Transition component
Component tmp;
foreach(Component comp in components) {
if(comp is T) tmp = comp;
break;
}
components.Remove(tmp);
}
}
}
class GameObject {
// separate variable for the transition component
private Transition transition;
private List<Component> components;
public T GetComponent<T>() where T : Component {
// if a transition is requestet just return it
if(typeof(T) == typeof(Transition)) {
return transition;
}
// else: search the requested component in the list
foreach(Component comp in components) {
if(comp is T) return (T)comp;
}
// return null or throw exception when not found
return null;
}
public void RemoveComponent<T>() where T : Component {
if(typeof(T) != typeof(Transition)) {
// only remove the componenent if it's not a Transition component
Component tmp;
foreach(Component comp in components) {
if(comp is T) tmp = comp;
break;
}
components.Remove(tmp);
}
}
}
类游戏对象{
//转换组件的单独变量
私人过渡;
私有列表组件;
public T GetComponent(),其中T:Component{
//如果转换是请求集,只需返回它
if(类型(T)=类型(过渡)){
回归过渡;
}
//else:在列表中搜索请求的组件
foreach(组件中的组件comp){
如果(comp为T)返回(T)comp;
}
//未找到时返回null或引发异常
返回null;
}
public void RemoveComponent(),其中T:Component{
if(类型(T)!=类型(转换)){
//仅当组件不是转换组件时才移除该组件
成分tmp;
foreach(组件中的组件comp){
如果(comp为T),则tmp=comp;
打破
}
组件。移除(tmp);
}
}
}
似乎您正在尝试创建一个称为实体组件模型的模型。我建议分析一些使用该模型的引擎,例如
(游戏对象)和(组件集合)应该是您特别感兴趣的
将转换组件保留为单独的字段,并将其存储在组件列表中,这可能是一个好主意。因为它是每个游戏对象的一部分,所以您可以为直接访问该组件提供单独的getter属性,以绕过任何查找开销
因为您似乎只允许每个类型有一个组件,所以将组件存储在字典中将是非常有效的。与迭代组件和进行类型比较相比,这将提供真正快速的查找
第二种方法的稍微修改版本:
class GameObject
{
private readonly Transition transition = new Transition();
private readonly Dictionary<Type, Component> components = new Dictionary<Type, Component>();
public GameObject()
{
AddComponent(transition);
}
public Transition Transition { get { return transition; } }
public T GetComponent<T>() where T : Component
{
Component component;
if (components.TryGetValue(typeof (T), out component))
{
return (T) component;
}
return null;
}
public void AddComponent<T>(T component) where T : Component
{
Type type = typeof (T);
if (!components.ContainsKey(type))
{
components.Add(type, component);
}
}
public void RemoveComponent<T>() where T : Component
{
if (typeof(T) != typeof(Transition))
{
components.Remove(typeof (T));
}
}
}
类游戏对象
{
私有只读转换=新转换();
私有只读字典组件=新字典();
公共游戏对象()
{
添加组件(转换);
}
公共转换转换{get{return Transition;}}
public T GetComponent(),其中T:Component
{
组分;
if(组件TryGetValue(类型(T),输出组件))
{
返回(T)分量;
}
返回null;
}
public void AddComponent(T component),其中T:component
{
类型=类型(T);
如果(!components.ContainsKey(类型))
{
组件。添加(类型、组件);
}
}
public void RemoveComponent(),其中T:Component
{
if(类型(T)!=类型(转换))
{
组件。移除(类型(T));
}
}
}
似乎您正在尝试创建一个称为实体组件模型的模型。我建议分析一些使用该模型的引擎,例如
(游戏对象)和(组件集合)应该是您特别感兴趣的
将转换组件保留为单独的字段,并将其存储在组件列表中,这可能是一个好主意。因为它是每个游戏对象的一部分,所以您可以为直接访问该组件提供单独的getter属性,以绕过任何查找开销
因为您似乎只允许每个类型有一个组件,所以将组件存储在字典中将是非常有效的。与迭代组件和进行类型比较相比,这将提供真正快速的查找
第二种方法的稍微修改版本:
class GameObject
{
private readonly Transition transition = new Transition();
private readonly Dictionary<Type, Component> components = new Dictionary<Type, Component>();
public GameObject()
{
AddComponent(transition);
}
public Transition Transition { get { return transition; } }
public T GetComponent<T>() where T : Component
{
Component component;
if (components.TryGetValue(typeof (T), out component))
{
return (T) component;
}
return null;
}
public void AddComponent<T>(T component) where T : Component
{
Type type = typeof (T);
if (!components.ContainsKey(type))
{
components.Add(type, component);
}
}
public void RemoveComponent<T>() where T : Component
{
if (typeof(T) != typeof(Transition))
{
components.Remove(typeof (T));
}
}
}
类游戏对象
{
私有只读转换=新转换();
私有只读字典组件=新字典();
公共游戏对象()
{
添加组件(转换);
}
公共转换转换{get{return Transition;}}
public T GetComponent(),其中T:Component
{
组分;
if(组件TryGetValue(类型(T),输出组件))
{
返回(T)分量;
}
返回null;
}
public void AddComponent(T component),其中T:component
{
类型=类型(T);
如果(!components.ContainsKey(类型))
{
组件。添加(类型、组件);
}
}
public void RemoveComponent(),其中T:Component
{
if(类型(T)!=类型(转换))
{
组件。移除(类型(T));
}
}
}
W