1 public void AddStates() 2 { 3 4 var subTypeQuery = from t in Assembly.GetExecutingAssembly().GetTypes() 5 where IsSubClassOf(t, typeof(State)) 6 select t; 7 8 foreach (var type in subTypeQuery) 9 { 10 var stateObj = Activator.CreateInstance(type) as State; 11 stateObj.Action(); 12 if (!states.Contains(stateObj)) 13 states.Add(stateObj); 14 } 15 } 16 17 static bool IsSubClassOf(Type type, Type baseType) 18 { 19 var b = type.BaseType; 20 while (b != null) 21 { 22 if (b.Equals(baseType)) 23 { 24 return true; 25 } 26 b = b.BaseType; 27 } 28 return false; 29 }
时间: 2024-10-29 12:42:14